My .HTACCESS Issues (Final Ones) - .htaccess

I explained neatly.
This Code 1 works properly with my INDEX pages (id est example.com/C Index of Terms starting with the letter C, instead of opening example.com/?open=Encyclopedia&letter=C):
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ((LJ).*) /?open=encyclopedia&letter=$2 [NE,L,QSA] # LJ Double-Letter Fix
RewriteRule ((NJ).*) /?open=encyclopedia&letter=$2 [NE,L,QSA] # NJ Double-Letter Fix
RewriteRule ((D\xC5\xBD).*) /?open=encyclopedia&letter=$2 [NE,L,QSA] # DŽ Double-Letter Fix
RewriteRule (([\x00-\xF48FBFBF].|\w).*) /?open=encyclopedia&letter=$2 [NE,L,QSA] # A-O-S Single-Letter Fix; Loads every other single-letter letter
It works finally fine with all the Index letters (including Š/Đ/Č/Ć/Ž and LJ/NJ/DŽ) and without errors. Now I have this Code 2:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([A-Z](?:[^\x00-\xF48FBFBF]+|[A-Z])?).*$ /?open=encyclopedia&letter=$1&term=$0 [B,NE,L,QSA]
Which also works finally fine with all the Terms (id est example.com/Astronomy: Term page for explanation for Astronomy, instead of opening example.com/?open=Encyclopedia&letter=A&term=Astronomy), but it:
Issue #1: won't open any Term page starting with Š/Đ/Č/Ć/Ž... Example:
• example.com/Đavo should redirect to example.com/?open=Encyclopedia&letter=Đ&term=Đavo
• example.com/Škorpija (Astrologija) should redirect to example.com/?open=Encyclopedia&letter=Š&term=Škorpija (Astrologija)
• example.com/Đžin should redirect to example.com/?open=Encyclopedia&letter=DŽ&term=Džin
I have not tested it with NJ/LJ/DŽ yet.
Issue #2:
Code 1 and Code 2 should be merged together and working. I tried to do it, but without success.

Related

example.com/en to behave as example.com?lang=en to example.com/en - htaccess

I added translations to my website and it uses the GET variable lang from the URL such as example.com?lang=en to show the right language.
I would like to make it look like example.com/en.
Also example.com/somePage.php?lang=en should likewise look like example.com/en/somePage.php
The language parameter would always be 2 letters right after the domain, so always example.com/xx.../.../...whatever-url
If nothing is given (For example example.com or example.com/whatever-url) then en is default
So I believe we need to take into account that if there's a folder or file called en or whatever, it should give priority to that folder / file. Not sure how to handle all the required filtering however.
I tried that, which fails (says page not found when I go to /en)
BUT example.com/en/index.php works.
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+([^/]+)/\?lang=([^\s&]+) [NC]
RewriteRule ^ /%2/%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([^/]+)/?$ $2/?lang=$1 [L,QSA]

htaccess cookie value is directory redirect

I have a cookie name=dir and value=test. I want htaccess to check if that value exist as a directory and redirect based on that.
RewriteCond /var/www/whatever/%{HTTP_COOKIE:dir} -d
RewriteRule ^(.*)$ example.com [R]
I know it would be possible with a RewriteMap, but I have no access to the conf file and RewriteMaps must be defined there, not in htaccess. A pure mod_rewrite solution would be best, because the module for setenv isn't enabled either. I've tried and googled, but to no avail.
Something like %{HTTP:header} but for cookies would be ideal, but Apache doesn't do that.
You have to match against %{HTTP_COOKIE} in a seperate RewriteCond
RewriteCond %{HTTP_COOKIE} ^dir=(.+)$
RewriteCond /var/www/whatever/%1 -d
RewriteRule ^(.*)$ example.com [R]
#starkeen: I wasn't aware that i could use %1 in RewriteCond, thought it was for RewriteRule only. Your Answer works perfectly exept for 2 things:
A. Regex. %{HTTP_COOKIE} is a String that can have 3 cases in this Situation:
Case 1: dir=abc - Your Regex works
Case 2: dir=abc; cookie1=v1 - Your Rexex does not work
Case 3: cookie1=v1; dir=abc; cookie2=v2 - Your Rexex does not work
Also important: it might be something like dir_save=v1; x_dir=v2; dir=abs, so something like
RewriteCond %{HTTP_COOKIE} ^.*dir=(.*).*$
will not work either.
So ... Start with [NEWLINE] or ';[SPACE]' and end with [LINE-END] or ';'
RewirteCond %{HTTP_COOKIE} (^|;\ )dir=(.*)(;|$)
RewriteCond %2 -d
RewriteRule ^(.*)$ example.com [R]
B. Also check for strange values of the cookie dir, like empty or '\' or '.' or '..\' and so on. On Windows of cause '\' ...
RewirteCond %{HTTP_COOKIE} (^|\ ;)dir=(.*)(;|$)
# must be a directory
RewriteCond %2 -d
# must not be empty
RewriteCond %2 !^$
# must not contain dot
RewriteCond %2 !^.*\..*$
# must not contain /
RewriteCond %2 !^.*/.*$
# must not contain \
RewriteCond %2 !^.*\\.*$
RewriteRule ^(.*)$ example.com [R]
#all: sorry for answering my own question (also starkeen showed a vital part i wasn't aware of) and also sorry for being so rigorous about regex and strange cookie values, but such things can cost you hours to fiddle out in the worst case. I've allready made such mistakes and learned the hard way.

htaccess codes did not work

This is EXACTLY the same case as: (htaccess) How to prevent a file from DIRECT URL ACCESS?
But, no one of codes provided by answers work for me. I tried 1 by 1, then tried to combine, but still not works. Here is my code:
# prevent direct image url access
# ----------
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)://(www\.)?example\.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)://(www\.)?example\.com.*$ [NC]
# this not works
RewriteRule \.(png|gif|jpe?g)$ - [F]
# and this
RewriteRule \.(png|gif|jpe?g)$ - [F,NC]
# and this
RewriteRule \.(png|gif|jpe?g)$ https://example.com/wp-login.php [NC,R,L]
# even by combining them
# ----------
# /prevent direct image url access
The case simulation:
index.php has <img src="test.png" alt=""> and should be normally accessible. The requirement is: http://example.com/test.png shouldn't be accessible.
I use WordPress in wp-engine, and i think WordPres's default rewrite doesn't cause the problem since the code from answers are placed above WordPress rewrite.
UPDATE
I use PHP Version 5.5.9-1ubuntu4.14 on Apache 2 on wp engine
Your rules basically work for me, except for one thing:
The (s) is not doing what you think it does.
RewriteCond %{HTTP_REFERER} !^http(s)://(www\.)?example\.com [NC]
With parentheses you define a group, which doesn't make any sense at this point. If you remove the (s), it works for http.
If you want to use https too you have to write it like this:
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com [NC]
The ? will make the preceding character (or group, if in parentheses) optional.

hide html extension + redirect .html version + special case exception

I have a hard time understanding htaccess mod_rewrite and while I found several related questions + answers, I unfortunately can't get my very specific situation to work correctly (mod_rewrite is even after hours of searching a book of seven seals to me to be honest)
I have foo.html and bar.html within my root directory. Now, I'd like to have foo.html as the default directory index (solved, easy), but from there I do not get it.
What I want to achieve is:
hiding the .html extensions
user should be able to type /bar to get /bar.html without seeing the .html (for every .html)
301 redirecting .html version
user should be able to type /bar.html and see /bar in the url (avoid duplicate, for every .html)
The most tricky part:
As foo.html is default directory index, typing / already shows (transparently) /foo.html, but I need typing /foo.html to resolve to / as well as typing /foo to resolve to /
Try putting these rules in the htaccess file in your document root:
RewriteEngine
# 1. hiding the .html extensions
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^ /%1.html [L]
# 2. 301 redirecting .html version
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.html
RewriteCond %1 !foo$
RewriteRule ^ /%1 [L,R=301]
# 3. typing /foo.html to resolve to / as well as typing /foo to resolve to /
RewriteCond %{REQUEST_URI} ^/(.*?/?)foo(\.html)?$
RewriteRule ^ /%1 [L,R=301]
Also, make sure you have Multiviews turned off:
Options -Multiviews
The first rule has 2 conditions, the first groups the URI everything between the first / and a possible last /. The references it using %1 in the next condition which sees if the /path/to/www/document/root/%1.html is a file that exists. If both are true, it internally rewrites the URI to include a .html at the end.
The second rule has 2 conditions, the first matches against the actual request as opposed to the URI (which can change as the rules are being applied and rewrites happen). It sees if there's a request that ends with .html, and if so, the second condition makes sure that it isn't foo.html request (since the last rule handles that). If both are true, then it redirects the browser to the part of the URI without the html, again using %1 to reference the grouping in the match from the first condition ([^\ ]+).
The last rule checks if the request is for a foo or foo.html. If so, redirect removing that part of the URI.
I am using the one below and it works except that, in some strange case when working with a WARRANTY script for a client it just won't do the thing.
However; the other scripts that calls external scripts via iFrame in a sub-folder seems to work with the exception of the WARRANTY script. ...
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_END} =1
RewriteRule ^ - [L,NS]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*?)\.(php|html?)$ $1 [R=301,NC,NS]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)$ $1.html [L,E=END:1,NS]
RewriteCond %{REQUEST_FILENAME}\.htm -f
RewriteRule (.*)$ $1.htm [L,E=END:1,NS]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*)$ $1.php [L,E=END:1,NS]
I hope this Helps.

htaccess issue including period/fullstop (.)

I currently have this .htaccess rule the works fine:
RewriteRule ^instructor/([A-Za-z0-9-]+)$ instructor.php?username=$1 [NC,L]
However, when I attempt to add a period into the mix a lot of the rules on the site break so I am assuming the character isn't escaped correctly:
RewriteRule ^instructor/([A-Za-z0-9-\.]+)$ instructor.php?username=$1 [NC,L]
Anyone point me in the right direction please?
Update
It appears to be something to do with the directory structure.
Another selection of rules that apply to this site are the following:
## Registration
RewriteRule ^instructor/register/?$ instructor-form/index.php [L]
RewriteRule ^instructor/register/stage([1-5]+)$ instructor-form/stage$1.php [L]
These work fine (the directory here is instructor-form/
However, there is also a directory called instructor/ which these rules point to:
RewriteRule ^instructor/dashboard/?$ instructor/index.php [L]
RewriteRule ^instructor/account-details/?$ instructor/account-details.php [L]
RewriteRule ^instructor/change-password/?$ instructor/change-password.php [L]
These are the rules that are affected when adding the . into the first rule. The rules are all in order and work fine without the . in the [A-Za-z0-9-] char block. When added the physical instructor/ folder seems inaccessible.
You wrote so much text in your question but forgot to mention important details: what is actually broken? Because I do not have clear answer for that I will be speculating here based on the information you have provided so far.
RewriteRule ^instructor/([A-Za-z0-9-\.]+)$ instructor.php?username=$1 [NC,L]
The problem with this rule is that it will also rewrite already rewritten php files: instructor/index.php, instructor/account-details.php, instructor/change-password.php etc.
I think you are relaying on [L] flag too much .. or do not really know how mod_rewrite and [L] flag work. And that is why you are having this issue -- your rule with a dot in pattern rewrites already rewritten URLs.
Useful link: RewriteRule Last [L] flag not working?
You need to add some condition (global rule or condition specific to this rule only) to prevent rewriting already rewritten URLs or existing files.
1. Global rule -- place it somewhere on the top before other rules. Keep in mind that this may not work as intended depending on your website structure and rewrite logic (e.g. when you need to actually rewrite requests to already existing files or folders):
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
2. Condition specific to that rule only:
a) do not rewrite if requested URI is physical file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^instructor/([A-Za-z0-9-\.]+)$ instructor.php?username=$1 [NC,L]
OR
b) do not rewrite .php files
RewriteCond %{REQUEST_URI} !.+\.php$
RewriteRule ^instructor/([A-Za-z0-9-\.]+)$ instructor.php?username=$1 [NC,L]

Resources