I want to make my htaccess file redirect all traffic except that to /images, /css, and /javascripts to the cgi-bin.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^images/.*$
RewriteCond %{REQUEST_URI} !^css/.*$
RewriteCond %{REQUEST_URI} !^javascript/.*$
RewriteCond %{REQUEST_URI} !^cgi-bin/.*$
RewriteRule (.*) /cgi-bin/$1 [L]
I want a request for: example.com/index to retrieve example.com/cgi-bin/index
but I also want a request for: example.com/images/foo.png to retrieve example.com/images/foo.png
What is wrong with my file? I keep getting this error: Request exceeded the limit of 10 internal redirects due to probable configuration error.
You are missing the leading / in all of your RewriteConds, which are required, though they should not be used for RewriteRule in .htaccess, which does cause confusion.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/images/ [NC]
RewriteCond %{REQUEST_URI} !^/css/ [NC]
RewriteCond %{REQUEST_URI} !^/javascript/ [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/ [NC]
RewriteRule (.*) /cgi-bin/$1 [L]
You also don't need the .*$ so I removed them to make it more efficient.
Related
I try to force a user which is coming via a specific domain example-page.com to always get redirected to /countdown
My approach was the the following:
RewriteCond %{HTTP_HOST} ^example-page\.com [NC, OR]
RewriteCond %{HTTP_HOST} ^www.example-page\.com [NC]
RewriteCond %{REQUEST_URI} !^/countdown
RewriteRule ^(.*)$ https://www.example-page.com/countdown [L,R=301]
But for some reason I always get a "Too many redirects error".
I get redirected to the https://www.example-page.com/countdown but somehow the RewriteCond is triggered again but it should not because of the RewriteCond %{REQUEST_URI} !^/countdown.
You may try this rule:
RewriteCond %{HTTP_HOST} ^(?:www\.)?example-page\.com [NC]
RewriteCond %{THE_REQUEST} !\s/countdown [NC]
RewriteRule ^ https://www.example-page.com/countdown [L,R=301]
Make sure to test if after clearing your browser cache.
Could you please try following, with your shown samples. Please keep these Rules at top of your htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example-page\.com [NC]
RewriteCond %{REQUEST_URI} !^/countdown
RewriteRule ^ https://www.example-page.com/countdown [L,R=301]
Also why you are getting error because you have space between NC and OR(NC, OR) which is causing that 500 internal error.
So I'm having this website which is stored inside public_html/www/. Some other domains are stored in -for example- public_html/test/. In public_html I have a .htaccess file with a LOT of redirects in it, which all apply to this website inside /www/ folder. Inside the /www/ folder I have another .htaccess (I don't really know why, my colleague did this and he left me with this without information). Since I have no idea how .htaccess exactly works, I have no idea what to do and in which file.
What I need to do is to redirect domain.nl to https://www.domain.nl (some folders should be excluded, like you can see in the code below - emailtemplates, paymentupdate, autocode, nieuwsbrieven, etc.) But domain.nl is stored inside public_html/www/ folder, so it also needs a redirect to the subfolder (which should not be visible in web browser).
At this moment, in the root .htaccess file is the following:
# Use HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/emailtemplates
RewriteCond %{REQUEST_URI} !^/paymentupdate_ideal
RewriteCond %{REQUEST_URI} !^/autocode
RewriteCond %{REQUEST_URI} !^/nieuwsbrieven
RewriteCond %{REQUEST_URI} !^/bevestig-aanmelding-nieuwsbrief
RewriteCond %{REQUEST_URI} !^/bevestig-afspraak
RewriteCond %{REQUEST_URI} !^/images/personal
RewriteCond %{REQUEST_URI} !^/uploadify
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Link to subfolder
# RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
# Non-www to www
#activate rewrite engine
#RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.nl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Probably half of this code isn't even necessary, but I have absolutely no clue. The other .htaccess file, within the subfolder /www/, has the following text which I don't really understand (it contains more, but nothing relevant):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.nl/$1/ [L,R=301]
I'm sorry if I sound stupid. I've tried many things copied from internet, but unfortunately everything has its concequences, which results in other problems with our website (links that don't work anymore, such as). I think the reason it doesn't work all the time, is because it's stored in the subfolder and I don't know how to combine all those lines in one like it would work.
Can anybody help me explain how to fix this and what is going wrong at this moment? Because the website is now not redirecting to www. It's just redirecting to https which gives me a security error at this moment (because the SSL is stored on www.). SUPER thanks in advance!
My problem was wrong order of the checks. First, I redirected to https://, but when I did not use www., it did not redirect to https://www OR it redirected to https://www.www. I fixed my problem by re-ordering my rules, like below:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Permanent redirect to https://
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/excluded_folder1/
RewriteCond %{REQUEST_URI} !^/excluded_folder2/
RewriteCond %{REQUEST_URI} !^/excluded_folder3/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Use subfolder for domain
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
What I did was this .htaccess rule:
RewriteCond %{HTTP_HOST} ^(www.)?example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /static/somefolder/$1 [L]
In the folder is a static html and some css. The html now tries to load the css from root, which is clearly not want I want.
Actually I wanted all requests to be redirected, except for one folder.
So I then used:
RewriteCond %{HTTP_HOST} ^(www.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/downloads/.*
RewriteRule ^(.*)$ /static/somefolder/$1 [L]
But this throws an 'Internal Server Error'.
What did I miss here?
You need to exclude the destination you are rewriting to :
RewriteCond %{HTTP_HOST} ^(www.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/downloads/.*
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule ^(.*)$ /static/somefolder/$1 [L]
Otherwise you will get an infinite loop error because /static/somefolder/* also matches the pattern ^(.*)$
I'm trying to do a Rewrite subdomains to subdirectories on htaccess.
Searching here I did the rule below:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]
#Rewrite for my Framework
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
When I access the address: http://user.domain.com.br or the address: http://www.user.domain.com.br they're redirected without change the URL address to: http://www.domain.com.br/sd_user/
Well, fine. My problem is:
When someone access a adrees with haven't a folder, .htaccess redirect it to a specific folder. For example:
I haven't a folder called "sd_joecompany", then I want to redirect it to a default folder named "sd_system". If I access http://joecompany.domain.com.br/ and not exists the respective folder, this address may point to "sd_system" keeping the address.
With this code, when I access a subdomain without a respective folder, the following error appear:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /index.php.
Reason: Max-Forwards has reached zero - proxy loop?
I'm not sure about the clarity of my doubt, and I'm sorry for my poor english.
I know a little about .htaccess and these rules. Everithing on the code above, I gathered here!
Thank you since now!
If they are on the same server and have the same document root, then you really don't want to proxy at all. Remove the host part of the rule and get rid of the "P":
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]
#Rewrite for my Framework
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
Cleaned up some of the regex and got rid of the extra RewriteEngine On's.
I have my .htaccess file setup like the following. It's a simple concept where all requests get forwarded to the cgi-bin/controller.rb except for those requests for anything in /images, /css, /javascripts, or /cgi-bin
I also want the root to just be redirected to /index/show but that's the part which is erroring. It just keeps infinitely redirecting to /index/showindex/showindex/show...
Can anyone explain why my redirectmatch doesn't work?
RedirectMatch permanent ^/$ /index/show
ErrorDocument 404 /404
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/images/ [NC]
RewriteCond %{REQUEST_URI} !^/css/ [NC]
RewriteCond %{REQUEST_URI} !^/javascripts/ [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/ [NC]
RewriteRule (.*) /cgi-bin/controller.rb?%{QUERY_STRING}&page=$1 [L]
Avoid using RedirectMatch and RewriteRule at the same time. Those are different modules and are applied at different times in the request-flow. You can achieve the same using only RewriteRule's:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index/show [NC]
RewriteRule ^$ /index/show [L]
RewriteCond %{REQUEST_URI} !^/index/show [NC]
RewriteCond %{REQUEST_URI} !^/images/ [NC]
RewriteCond %{REQUEST_URI} !^/css/ [NC]
RewriteCond %{REQUEST_URI} !^/javascripts/ [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/ [NC]
RewriteRule (.*) /cgi-bin/controller.rb?page=$1 [L,QSA]
Optionally use RewriteRule ^$ /index/show [L,R=301] if you really want the redirect, instead of leaving the address bar nice and clean.
Also remember to clear the browsers cache, as the Permanent redirect are cached very aggressively.