Forum Moderators: phranque

Message Too Old, No Replies

if you wish to force the use of www instead of non-www

Apache HTTP

         

whaines

12:13 am on May 23, 2012 (gmt 0)

10+ Year Member



#running on port 80

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

or this correct

#running on port 80

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

Thank You,
whaines

incrediBILL

2:34 am on May 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Did you try them?

Did they work?

lucy24

6:46 am on May 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



or this correct


Neither. You need a single RewriteCond that says (in English) "if the requested host is NOT" exactly what you want it to be. There are many possible incorrect requests, and only one correct one.

This is one of the Top 20 questions-- at least-- in the Apache forum. Forums search should bring up several thousand earlier threads showing the exact punctuation needed.

Did they work?

Well, that's one question to ask. There are others ;)

g1smd

6:55 am on May 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's only a matter of days since someone presented a list of two or three options for www/non-www canonicalisation and asked which one was correct.

That thread has a whole bunch of useful information along with code with less issues.

The code with ^/ is intended for httpd.conf.

The second one is intended for use in htaccess.

Both have a significant list of issues that make them non-optimum.