Forum Moderators: phranque
I've seen a few versions; but before I plunge into changing my htaccess file I wanted to get it right:
version 1:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
version 2:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Although subtle differences, would version 2 try to redirect www.example.com to www.example.com and set in a loop?
If someone could tell me which one to use, that would be great.
Thanks.
[edited by: jdMorgan at 1:51 pm (utc) on Mar. 14, 2005]
[edit reason] Examplified and de-linked [/edit]
you've to use the 1st version to redirect http://example.com to http://www.example.com.
The 2nd Version redirects requests from www.example.com to www.example.com - which is the same and loops...
[edited by: jdMorgan at 1:49 pm (utc) on Mar. 14, 2005]
[edit reason] Examplified and de-linked [/edit]
I guess you can write 2 different ways for the server, either the "not" function or the positive one:
I ended up using the code below which seems to work very well:
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule (.*) [mydomain.com...] [R=301,L]
My concern with version 1 on my original post was that it looked like the RewriteCond would rewrite any page that had "mydomain.com" in the url regardless whether it was preceded with a "www".
The negative approach didn't seem to have that problem.
Am I looking at this right?
Thanks,
Rich
rewriteEngine on
rewriteCond %{HTTP_HOST}!^www\.example\.com
rewriteRule ^(.*)$ [example.com...] [R=301,L]
I have two domain names (one is a common misspelling of the other) pointed at the same IP. This causes any URL that does not have the canonical domain name to get fixed. So, example.com, exampple.com and www.exampple.com all get fixed to www.example.com.
You rule will fail with an "infinite" rewrite loop if your server receives an HTTP/1.0 request. In that case, the HOST variable will be blank, which does not match your desired domain. So your rule will generate a redirect. And the client will come back, again with a blank HOST, and your server will generate another redirect, ad infinitum, until the client or the server times out. Then the connection attempt will fail.
Add "RewriteCond %{HTTP_HOST} ." or "RewriteCond %{HTTP_HOST} !^$" ahead of your existing RewriteCond to prevent this (they're the same thing in positive and negative logic respectively, requiring a non-blank HOST header).
rshandy,
> My concern with version 1 on my original post was that it looked like the RewriteCond would rewrite any page that had "mydomain.com" in the url regardless whether it was preceded with a "www".
No, it will redirect only the non-www version, because the RewriteCond pattern is start-anchored and won't match of the hostname starts with "www". Look for the words "anchor" and "anchoring" in the mod_rewrite docs and the regular-expressions guide cited in our forum charter.
Jim
You can also also shorten it by using the postive-logic test for non-blank if you like:
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.co[b]m[/b]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
If you feel you must end-anchor the domain name, then use:
RewriteCond %{HTTP_HOST} !^www\.example\.co[b]m(:[0-9]{1,5}?$[/b]
This and my previous post are pretty good demonstrations of the subtleties you can get into with mod_rewrite, and just how precise it can be.
Jim
I don't think I need to anchor the domain so I'll remove the $:
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com [NC]
Also, could you please verify your statement regarding "NC".
Not sure where I found this, but I thought that
[NC] means 'No Case' (ie. not case sensitive)
I tested typing in all caps of mydomain and it does redirect it to the lowercase version.
Rich
It should redirect if you type *any* caps in the domain name, as long as you omit the [NC] flag. You are using a negative match here, so the redirect happens is the requested domain *does not* match the one given. If you add an [NC] flag, then an uppercase or mixed-case domain name will be accepted as a match, and no redirect will take place. For the sake of standardization, and keeping subsequent rules simple, it's better to accept only the correctly-spelled, correct-case domain name. Otherwise, redirect to fix it, and be dome with it. In this way, subsequent rules and scripts run later won't have to deal with uppercaase or mixed-case domain names.
It's also common for the browser to force the domain name to lowercase, so again in that case, the [NC] flag is not needed with a negative-match RewriteCond.
Jim