.htaccess WWW 301 redirect variations?

Basically I’m trying to figure out what the RewriteCond site address variables mean? Trying to decide which one is best/best 4 me/SEO. Does anyone know what the \ marks by the www & .com do? ! point? The last example has the www part…Is it just over kill or does it serve a purpose?

note: I’m want to do the 301 to www. Not 301 to non www. I’m just trying to understand the variables regarding these examples.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]


RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.test\.com [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [R=301,L]

We had the very same discussion earlier this week http://www.sitepoint.com/forums/showthread.php?799161-Htaccess-For-No-WWW


BTW. All your examples are exactly the same but use different example domains, which you would need to replace with your own domain.

So other than the domain names all being different and the fact I would need to enter my own domain name…there all do the same thing? Even tho the 2nd example has \ before .com and the 3rd example has and ! point…two \ marks and www in front of it? Apache code is new to me…So I guess what I’m really trying to figure out what does what with regards to some of these characters.

Okay so they are slightly different in the way they approach things, and the first one contains an error. Let me walk you through them.
To make it more obvious what’s happening I will change all domains in all rules to example.com

First up

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This one contains an error, the . in line example.com in the RewriteCond should have been escaped, i.e., it should have been example\.com. So

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

What this rule now does is rewrite every request for example.com (without the www) to [noparse]www.example.com[/noparse].
The (.*) isn’t the best idea ever, but I’ll get to that later

The second one

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\\.com [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

After fixing the error in the first one this one is now exactly the same but uses domain.com instead of mydomain.com

The third one

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\\.example\\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This one changes it around a bit.
If the request is not for [noparse]www.example.com[/noparse], rewrite to [noparse]www.example.com[/noparse]. This will rewrite all subdomains to www.
So if you have subdomains, like [noparse]my.example.com[/noparse] or something like that, you should be using the first rule because you don’t want to rewrite those.
I personally find 3 a bit overkill and would stick with the first one anyway.

As for the (.*), this is known as greedy regex in that it will capture everything and anything, while what you need here is already available in a variable from Apache, namely %{REQUEST_URI}. We’d better use that instead as that causes less work for Apache.

So, the ultimate version would be (IMHO)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\\.com [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

For a good tutorial on mod_rewrite and how all this stuff works, have a look at http://www.datakoncepts.com/seo

HTH :slight_smile:

Thanks! That’s just what I needed 2 know…Makes sense now. Thanks 4 the link 2. Great info.

Back again…I’m going back and fourth about doing the remove trailing slash mod. What ver/example of that code would you recommend? It would be 4 a simple clean site…No sub domains…5-6 directories.

What do you mean by “remove trailing slash mod”?

It’s another .htaccess mod… I guess it would be called a RewriteCond.


w/o trailing slash: http://www.site.com/products

Link 2 one of many sites with an “example”…again…they varry… http://www.crucialwebhost.com/blog/htaccess-apache-and-rewrites-oh-my/#remove-trailing-slash

Lots of sites use it…seomoz.org … redbox.comzappos.com … ext.

Something like

# If the current is not for an existing directory ...
RewriteCond %{REQUEST_FILENAME} !-d
# ... remove the trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]


It’s not workin 4 me…Maybe im entering it wrong? I’m entering it in my .htaccess file right after the code you provided above for the www rewrite. I’ve tryed it w/ and w/o the comments…Trailing slash’s remain.

If “products” is actually a directory you can’t remove the trailing slash, you can only do that for fake URLs (which you can show like extensionless files).

Hmm. This seems to be more of a hassle than it’s worth. How do they do it then? example: http://www.redbox.com/facts … I’ve seen lots of sites doing it…

They don’t have actual directories with that name, they use pretty URLs that are all routed through one index.php (or other programming language)

What you could do is rename the directory to something else, like facts-internal, and then rewrite all requests that want “facts” to “facts-interal”, but indeed that does seem to be more of a hassle than it’s worth.