homepage Welcome to WebmasterWorld Guest from 54.161.202.234
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Simple rewrite question
rshandy




msg:1520213
 4:10 pm on Mar 4, 2005 (gmt 0)

I just wanted to get confirmation on the correct code to create a http://example.com to http://www.example.com rewrite.

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]

 

Caterham




msg:1520214
 6:11 pm on Mar 13, 2005 (gmt 0)

Hi,

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]

jdMorgan




msg:1520215
 1:53 pm on Mar 14, 2005 (gmt 0)

Yes, the first version is correct, except that the literal period in the hostname pattern should be escaped:

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

Jim

rshandy




msg:1520216
 8:30 pm on Mar 14, 2005 (gmt 0)

Well, Thanks for responding. After I hadn't gotten any response for a few weeks, I went back and did more reading on the rewrite rules, namely, the apache url rewrite guide.

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

wruppert




msg:1520217
 8:55 pm on Mar 14, 2005 (gmt 0)

I use

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.

jdMorgan




msg:1520218
 10:07 pm on Mar 14, 2005 (gmt 0)

Wruppert,

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

rshandy




msg:1520219
 10:16 pm on Mar 14, 2005 (gmt 0)

Thanks Jim:

Thank makes perfect sense. With that said, is there anything wrong with the way I have the redirect written now?

Rich

jdMorgan




msg:1520220
 11:58 pm on Mar 14, 2005 (gmt 0)

You should not end-anchor the domain name, or the rule will fail if a port number is appended. I also suggest you *don't* block the redirect if someone types uppercase letters in your domain name, so remove the [NC] flag.

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]

That makes it several billionths of a second faster! ;)

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 will allow for the case where a port number is appended, e.g., "www.example.com:80/page.php", which is a perfectly-legal URL.

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

wruppert




msg:1520221
 4:00 am on Mar 15, 2005 (gmt 0)

jdMorgan,

Made the changes, works great. Thanks!

rshandy




msg:1520222
 1:26 pm on Mar 15, 2005 (gmt 0)

Thanks again Jim! It's amazing the myriad of sublteties the rewrite command brings.

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

jdMorgan




msg:1520223
 2:49 pm on Mar 15, 2005 (gmt 0)

> I tested typing in all caps of mydomain and it does redirect it to the lowercase version.

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved