This is the 1st time I am using stackoverflow, I usually find answers to my questions about development thanks to Google, or with the help of some friends. Not this time !
I am in trouble with a .htaccess file where I want to define a double rule.
Initally I had this code for URL rewriting :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php?query=$1&%{QUERY_STRING} [L]
It is working like a charm, but I saw that many of the pages of the website indexed by Google where duplicated on a subdomain of my website, so I decided to add this code in the .htaccess :
RewriteCond %{HTTP_HOST} !mydomain.com/$
RewriteRule ^(.*) http://www.mydomain.com/$1 [R=301,L]
Please notice that I have added this code, just after the RewriteEngine on.
The result is almost what I expect, the 301 redirection from subdomain such as mail.domain.com/mypage.html is working and leads to http://www.domain.com/mypage.html but the second rule for URL rewriting pages is like cancelled ! So all pages which use the second rule have a 404 error.
I have tested many different things but couldn’t figure out how to fix that ?
Anyone has a tip to help me success ?
Thanks in advance guys !
This condition always matches, since you are asking for a hostname that does not end in
mydomain.com/;%{HTTP_HOST}will never end in a slash, since it only contains the hostname with no path. Since the condition always matches, itsRewriteRuleis always in effect, and it is marked as[L](last rule). I have no idea why it does not result in an redirection loop, probably some short-circuit logic inmod_rewritethat stops the rewriting when the URL hasn’t been changed.What you probably want is this: