“Internal Server Error ” on invalid pages - .htaccess

Some lines of my htaccess file will be look like as follows
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.js$
RewriteCond %{REQUEST_URI} !\.images$
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteCond %{REQUEST_URI} !(\.jpg|\.gif|\.png)$ [NC]
RewriteRule ^(.*)$ user/$1 [L,QSA]
But when I go for a invalid url, so for mydomain.com/blablabla ,it's giving Internal Server Error. Is anything wrong with my .htaccess

Try adding these conditions right after RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteCond %{DOCUMENT_ROOT}/user/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/user/%1 -d
This checks if the rewritten URI would exist, if it doesn't, it won't do the rewrite, which is causing an internal loop if the URI doesn't exist.

Related

Remove multiple slashes but preserve trailing one

I have a rule in my .htaccess to remove multiple trailing slashes. It works:
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ (.*)//([^\ ]*)
RewriteRule ^ %2/%3 [R=301,L]
I also have a rule that adds one trailing slash if it's missing. On its own, it works, too:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
What I'm trying to achieve here is pretty obvious - remove multiple slashes, leave one. But with first rule applied, I'm getting no trailing slash on index page even when I specify it.
Please help me to combine these rules into one or at least get them to work properly. I tried to use this tool for debug but it is unable to process %{THE_REQUEST}.
I'll leave rest of .htaccess file for the reference, maybe it interferes. It wasn't written my be but I have to figure it out.
RewriteCond %{REQUEST_URI} !^styles
RewriteCond %{REQUEST_URI} !^css
RewriteCond %{REQUEST_URI} !\.css
RewriteCond %{REQUEST_URI} !^js
RewriteCond %{REQUEST_URI} !\.js
RewriteCond %{REQUEST_URI} !^images
RewriteCond %{REQUEST_URI} !^uss-js
RewriteCond %{REQUEST_URI} !^uss-css
RewriteCond %{REQUEST_URI} !^uss-img
RewriteCond %{REQUEST_URI} !^uploadedFiles
RewriteCond %{REQUEST_URI} !(\.ico|\.ICO)$
RewriteCond %{REQUEST_URI} !(\.jpg|\.JPG)$
RewriteCond %{REQUEST_URI} !(\.jpeg|\.JPEG)$
RewriteCond %{REQUEST_URI} !(\.png|\.PNG)$
RewriteCond %{REQUEST_URI} !(\.gif|\.GIF)$
RewriteCond %{REQUEST_URI} !(\.css|\.CSS)$
RewriteCond %{REQUEST_URI} !(\.js|\.JS)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?reference=$1 [L]
RewriteCond %{REQUEST_URI} (\.ico|\.ICO)$
RewriteCond %{REQUEST_URI} (\.jpg|\.JPG)$
RewriteCond %{REQUEST_URI} (\.jpeg|\.JPEG)$
RewriteCond %{REQUEST_URI} (\.png|\.PNG)$
RewriteCond %{REQUEST_URI} (\.gif|\.GIF)$
RewriteCond %{REQUEST_URI} (\.css|\.CSS)$
RewriteCond %{REQUEST_URI} (\.js|\.JS)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ [R=404]
I'm getting no trailing slash on index page
Yes you won't because your trailing slash adding rule has this condition:
RewriteCond %{REQUEST_FILENAME} !-f
Which means if request is for a valid file then don't execute.
You can add a trailing slash in your first rule also:
# remove multiple slashes from URL and add a trailing one
RewriteCond %{THE_REQUEST} //
RewriteRule ^(.+?)/?$ /$1/ [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=302,L,NE]

Weird behavior with multiple conditions

I have the following rules:
RewriteCond %{REQUEST_URI} !script.php
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule .* script.php?q=%1
RewriteCond %{REQUEST_URI} !script.php
RewriteRule (.*) script.php?q=$1
What I'm trying to accomplish here is that every access to http://example.com/?url=www.google.com is redirected to script.php?q=www.google.com and if there is no query string, relative path should be sent to script.php, for example in http://example.com/path, should be redirected to script.php?q=path. This doesn't seem to work with my rules, but I'm not sure why. Is this even possible ?
You can have your rules as this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^url=([^&]+) [NC]
RewriteRule ^/?$ script.php?q=%1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.+)$ script.php?q=$1 [L,QSA]

htaccess subdomain ssl wildcard not opening file

Ok So i have come across a problem with my htaccess and how to get it to work.
I have just purchased a ssl wildcard for my primary and sub domains.
I am with bluehost and they suggest adding this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomain/$1
RewriteCond %{HTTP_HOST} ^subdomain.example.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
At the moment all i have is this...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ https://www.domain.com/$1 [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
and that works however, i cannot get it so that when i open the file, it just opens the file with no redirects...
So If i open sub.domain.com/file/fil1.js it will open the index.php which is what i do not want...
but if i do /browse or something like that it works...
Ok after much mucking around and playing around it was the file request which wasnt including the subdomain folder, which was causing it to not find the file location. So I managed to do a manual check for the file, and this is what i got. This seems to do exactly what i want. Im not sure if there is a more efficient way, but this is what it got.
#-------------------SUB.DOMAIN.COM---------------------
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{DOCUMENT_ROOT}/sub_folder%{REQUEST_URI} -f
RewriteRule . %{DOCUMENT_ROOT}/sub_folder%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
#--------------------------------------------------------------
instead of
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
You need to change your second rule so that it only gets applied when the request is ^(/)?$, then you need to duplicate what bluehost tells you to do. Essentially, replace:
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
With what they tell you to use:
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomain/$1 [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteRule ^/?$ /sub_folder/index.php [L]

htaccess exception for subdomain

I have this line in my htaccess file: RewriteRule (.*) index.php
This catches all requests being sent to my site domain.com
Now i have created a subdomain blog.domain.com and i am getting an Internal server error. How can i add blog.domain.com as an exception in htaccess.
This is my whole htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} .*/http-bind
RewriteRule (.*) /http-bind [L]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.raw|/[^.]*)$ [NC]
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} !/google3ce18567a119af4.html
RewriteRule (.*) index.php
Add the following line to the top of your RewriteCond block.
RewriteCond %{HTTP_HOST} !^blog\.domain\.com
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.raw|/[^.]*)$ [NC]
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} !/google3ce18567a119af4.html
This will only pass when the request is not for blog.domain.com and meets all of your current conditions.
As a side, I would encourage you to do something more like the following as opposed to your specific file checks.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This ensures that the request is not an existing file or directory. It's easier to maintain than whitelisting specific resources (e.g. google3ce18567a119af4.html, /ow_updates/, etc).

htaccess mod rewrite - how to create virtual subdomains?

I would like to create virtual subdomains through htaccess in the following way.
Entering:
http://testuser.domain.com/1/2/3/
Should be processed as:
http://www.domain.com/user.php?id=testuser&var1=1&var2=2&var3=3
HOWEVER, this rewrite should not use user.php, but index.php, in case someone enters:
http://www.domain.com or http://domain.com
This is what I got so far, however it doesn't seem to work.
Any help from a mod rewrite expert would be much appreciated.
Thanks in advance.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]
Your rules are actually very close to doing what you want them to. The only problem that I can see is in your test patterns for your two RewriteRule statements. Currently, you have
RewriteRule ^(.*)/?(.*)/?(.*)/?$ ...
...which happens to be equivalent to this:
RewriteRule ^(.*)$
This is because everything past the first capture group can match nothing and still be considered a match, so that first group is greedy and matches the whole input string without needing to defer to the other parts of the pattern.
Since the capture groups shouldn't capture forward slashes anyway, as they're being used as a variable delimiter here, the straightforward fix is to change them to [^/]*, as so:
Edit: I also modified the RewriteCond set in the second group to ignore the !-f condition in the case of /index.php, which will happen if you request the subdomain without anything after the domain.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]

Resources