Something with my .htaccess file is creating a rule loop of some sort when a certain condition exists. Here is the code in question:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.+)/?$ /%1.php?o=$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteRule ^$ /%1.php [L,NC,QSA]
</IfModule>
Normally what this does is:
test.example.com -> example.com/test.php
test.example.com/test2 -> example.com/test.php?o=test2
Thats fine, except when the "test" subdomain part points to a nonexistent file, I get an internal redirect error, specifically this:
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: [retracted]
Any help is appreciated.
Yes it will cause looping because RewriteCond %{REQUEST_FILENAME} !-f will still be true for /test.php when /test.php doesn't exist.
You can use this code to prevent looping:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# don't redirect after one internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.+?)/?$ /%1.php?o=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^$ /%1.php [L]
</IfModule>
Related
I have a site with dynamic subdomains based on registered usernames that are parsed using php and for that my htaccess works without a problem.
Here are some example on how the redirect works from $_SERVER
thecspace.com/blogs
["QUERY_STRING"] => string(21) "template=www&q=blogs/"
http://www.thecspace.com/blogs/test-pp-blog-2904
["QUERY_STRING"] => string(38) "template=www&q=blogs/test-pp-blog-2904"
http://danvlad.thecspace.com/blogs/test-pp-blog-2904
["QUERY_STRING"] => string(47) "template=danvlad&q=blogs/test-pp-blog-2904"
CURRENT HTACCESS
RewriteCond %{HTTP_HOST} ^thecspace\.com$ [NC]
RewriteRule ^(.*)$ http://www.thecspace.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)?\.thecspace\.com
RewriteRule ^$ index.php?template=%2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)?\.thecspace\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?template=%2&q=$1 [L,QSA]
Now comes the hard part. I want to allow custom domains to use the same rules. Let's say devdemogroup have the domain devdemogroup.com. Once they change DNS to point to domain.com I want to be able to allow that domain to use current code from domain.com
So, in theory a request to www.danvlad.com would be a this query string
["QUERY_STRING"] => string(47) "template=danvlad"
and www.danvlad.com/blogs/
["QUERY_STRING"] => string(47) "template=danvlad&q=blogs"
I've tried this but it won't work
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.thecspace\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.thecspace\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteRule ^(.*)$ /index.php?template=%1?q=$1 [L,QSA]
Can anyone help?
Thank you
Actually your last RewriteRule is without conditions, use it like this:
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?template=%1?q=$1 [L,QSA]
Final solution
Route53
thecspace.com Zone
danvlad .thecspace.com CNAME www.thecspace.com
www.danvlad .thecspace.com CNAME www.thecspace.com
danvlad Zone
www.danvlad.com CNAME www.thecspace.com
Options FollowSymLinks
AllowOverride All
Allow from all
DirectoryIndex index.php
# Redirect thecspace.com and cspace.thecspace.com to www.thecspace.com
RewriteCond %{HTTP_HOST} ^thecspace\.com$ [NC] [OR]
RewriteCond %{HTTP_POST} ^cspace.thecspace.com$ [NC]
RewriteRule ^(.*)$ http://www.thecspace.com/$1 [R,L,QSA]
# Redirect abc.thecspace.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.abc.thecspace.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.abc.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.thecspace.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME) !^$
RewriteRule ^(.*)$ /index.php?q=$1 [L]
Maybe it helps someone.
I have the following code that redirects any http:// request to https:// - This work great, but how would I edit this to make an exception for one page e.g. mydomain.com/sitemap-news.xml - and keep this as http:// ?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As listed in comments here is how my entrie .htaccess looks
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^sitemap-news\.xml$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} off
RewriteRule !^sitemap-news\.xml$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NE]
You can make an exception for sitemap file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^sitemap-news\.xml$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} off
RewriteRule !^sitemap-news\.xml$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I am trying to use htaccess to make a subdirectory the root for an additional domain. I currently have:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/$1
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteRule ^(/)?$ test/index.php [L]
It works great and hides the 'test' directory in the URL. But, when a file in a subdirectory is requested it shows the 'test' directory. For example, instead of thedrive.co/somefolder it shows thedrive.co/test/somefolder.
How can I get it to not display that?
Insert a 301 rule right at the top below RewriteEngine on line:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?radioreformation\.co$ [NC]
RewriteCond %{THE_REQUEST} \s/+radioreformation.com/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteCond %{REQUEST_URI} !^/radioreformation.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /radioreformation.com/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteRule ^(/)?$ radioreformation.com/index.php [L]
This will redirect thedrive.co/test/somefolder to thedrive.co/somefolder
I have a shared hosting with multiple domain hosted on it. In the root folder of my hosting exists a .htaccess (say is htaccess1). Code in ht1 is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain/index.php [L]
These rules are there so that only urls with www.mydomain.com use all files of mydomain folder. I am new to url rewriting so I don't understand the meaning of what each line does. Another .htaccess (say htaccess2) file exists in mydomain folder. Code is htaccess2 is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)?/$ try.php?id=$1
RewriteRule ^([0-9]+)?$ try.php?id=$1
RewriteRule ^post/([0-9]+)?/$ post.php?id=$1
RewriteRule ^post/([0-9]+)?$ post.php?id=$1
In this file, line 6,7 works fine and redirect requests with numeric parameters to try.php but lines 8,9 doesn't work and gives a 404 page not found error, exact error string is :
The requested URL /mydomain/post/1233445 was not found on this server.
I doubt that some effect of htaccess1 is creating this problem as I tried many variations of regex in line 8,9. Please help.
Main .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^$ /mydomain/index.php [L]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Added required options.
mydomain .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ try.php?id=$1 [L,QSA]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Removed a redundant rule.
Moved specific rule above the generic rule.
Added required options.
I know the topic "How to force HTTPS + WWW" is often discussed and solved, and in general it works for me.
But as I now got a specific predefined .htaccess from CakePHP I do not know how to include it.
.htaccess for CakePHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
If I put the normal code for HTTPS/WWW Forcing in front or backwards to that code, it does not work properly because all requests are set to root directory and not to e.g. /contact.
Normally I am using:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^ https://www.mydomain.com%{REQUEST_URI} [R=301]
But you can not just include that above...
Could anybody please help me including HTTPS/WWW Forcing in the above .htaccess?
The generic way is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
No need to hard-code domain name. Also request will not redirect to root when switching from http to https
Thanks LazyOne, this maybe working, but for me it often ended up in "mydomain.com/redirect:/app/webroot/index.php" which was really strange. But maybe this is due to the "{REQUEST_URI}" because I had to change my
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
to
RewriteRule ^(.*)$ index.php [QSA,L]
due to strange problems with redirect (no idea what happend, CakePHP suddenly requestet a "Redirect:Controller" as also described here http://groups.google.com/group/croogo/browse_thread/thread/55539dabfd0191fd?pli=1 - any idea about this?).
It is now working with this code:
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) https://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
This is how it should be:
RewriteEngine On
# force https and www.
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^ https://www.mydomain.com%{REQUEST_URI} [R=301,L]
# route all requests for non-existing resources to CakePHP
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
another way might be to force SSL using a component ? http://bakery.cakephp.org/articles/lemon/2008/07/07/component-for-forcing-a-secure-connection