Forum Moderators: phranque

Message Too Old, No Replies

.htaccess file a mess

I don't really know what I'm doing but I've had a crack at it

         

EddNCFC

8:57 am on Jun 19, 2012 (gmt 0)

10+ Year Member



Hi,

I've recently bought a better version of a domain I was using.

Old version: www.exampl.com
New version: www.example.com

Now I've set up an .htaccess file to 301 Old version to New version and at the same time forward the non-www to www. However, the .htaccess file looks a mess. Have I don it correctly?

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

phranque

9:12 am on Jun 19, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, EddNCFC!

better to do that with a single rule - if HTTP_HOST is not exactly www.example.com then redirect to ...

EddNCFC

9:19 am on Jun 19, 2012 (gmt 0)

10+ Year Member



Hi, thank you for the reply. I'm more of an MS guy and am just starting to learn Apache. What would the .htaccess file look like? regards

EddNCFC

9:58 am on Jun 19, 2012 (gmt 0)

10+ Year Member



Hi,

OK, Is this it:

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

phranque

11:57 am on Jun 19, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the protocol won't appear in the HTTP_HOST environment variable and to make it "not exactly" you want to start and end anchor the pattern and you want to escape the literal periods since a "." in a regular expression mean "any character".
and there's one more little thing - true HTTP/1.0 clients do not send a Hostname header so you must also check for an undefined HTTP_HOST to prevent a redirection loop.
(extended HTTP/1.0 user agents such as googlebot and modern HTTP/1.1 clients will not cause this problem.)
all that means you should use a pattern more like:

!^(www\.example\.com)?$

(which means "defined and not exactly 'www.example.com'")

you might also want to read the section on Modifying the Query String to see if it applies:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

EddNCFC

12:03 pm on Jun 19, 2012 (gmt 0)

10+ Year Member



Thank you for your feedback, OK so it would look like this:

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

phranque

12:42 pm on Jun 19, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



assuming you're ok with arbitrary query strings being passed in the redirect...

EddNCFC

3:17 pm on Jun 19, 2012 (gmt 0)

10+ Year Member



OK, that's all great, thank you.

Another thing, on the same site I have a WordPress news section with a .htaccess file in the WordPress directory containing:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]
</IfModule>

# END WordPress

This does not redirect to the new site and does not convert non www to www. How would I add in a line to point to new site and non www to www and still retain the original redirect?

g1smd

8:04 pm on Jun 19, 2012 (gmt 0)

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



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


Two corrections!

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


That's the fifth time this code has been asked about in the last 24 hours.

phranque

10:21 pm on Jun 19, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



http://tools.ietf.org/html/rfc3986#section-3.2.2:
The host subcomponent is case-insensitive.


therefore the [NC] is indicated in the "RewriteCond %{HTTP_HOST} ..." directive.

EddNCFC

9:08 am on Jun 20, 2012 (gmt 0)

10+ Year Member



Thank you, it is now live.