Forum Moderators: phranque
We found out quite sometime ago that we needed to redirect our site from either the non-www to the www or visa versa, we chose to go with the non-www to www so I downloaded the .htaccess file sitting in the /public_html directory for our site and added the following lines to the top of the file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oursite\.com [nc]
RewriteRule (.*) [oursite.com...] [R=301,L]
This appeared to work perfectly. Over time we have added other redirects via Cpanel for pages changed or deleted from the site, example: [oursite.com...]
-----> [oursite.com...] this too appeared to work as expected.
However, when checking the server headers with web bug and other tools, I noticed that in actuality this happens:
User IP Address: 172.165.162.3#1 Server Response: [oursite.com...]
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Date: Wed, 24 May 2006 01:47:19 GMT
Server: Apache/1.3.34 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.4.1 FrontPage/5.0.2.2635 mod_ssl/2.8.25 OpenSSL/0.9.7a
Location: [oursite.com...]
Connection: close
Content-Type: text/html; charset=iso-8859-1
Redirect Target: [oursite.com...]
#2 Server Response: [oursite.com...]
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Server: Apache/1.3.34 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.4.1 FrontPage/5.0.2.2635 mod_ssl/2.8.25 OpenSSL/0.9.7a
Location: [oursite.com...]
Connection: close
Content-Type: text/html; charset=iso-8859-1
Redirect Target: [oursite.com...]
#3 Server Response: [oursite.com...]
HTTP Status Code: HTTP/1.1 200 OK
ETag: "1834b-4e17-4436d14d"
Content-Length: 19991
Content-Type: text/html
Last-Modified: Fri, 07 Apr 2006 20:53:33 GMT
My question is this...how to I get it to go from:
The non-www page(oursite.com/garden/test.html) directly to the correct www page (www.oursite.com/garden/testing.html),without the non www page(oursite.com/garden/test.html)first being redirected to the www page with the same name (www.oursite.com/garden/test.html), then that page being redirected again,I assume by the redirect in CPanel,(oursite.com/garden/test.html --->www.oursite.com/garden/testing.html) to the correct www page.
I do not want to remove the 301 for non www to www so how do I accomplish this without having multiple redirects for the same page or does it matter?
I do hope I have not confused everyone with the way this is explained and hope that someone can shed some light on what has to be done.
Thanks
{edited to correct spelling}
If you use mod_rewrite for the per-page redirects in .htaccess, and place them above the domain redirect, then your problem will disappear. To emphasize: You must use mod_rewrite for all redirects; If you mix mod_alias and mod_rewrite directives, then --in a normal server configuration-- the mod_alias directives will all execute first. Because mod_rewrite is *required* if you want to do conditional rewrites/redirects, this establishes the need to use mod_rewrite for all redirects.
Once you've done this, if an incorrect page URL is requested from either www- or non-www domain, it will be redirected to the correct page, but always in the 'correct' www domain. But, if the requested URL does not match a page that is to be redirected, the code will fall through to the domain redirect at the end, where the domain redirect will be invoked if the page is correct, but the domain is incorrect.
# Setup
Options +FollowSymLinks
RewriteEngine on
#
# Redirect old page URL-paths to new page URLs, always in canonical "www" domain
RewriteRule ^foo\.html$ http://www.example.com/bar.php [R=301,L]
RewriteRule ^quux\.php$ http://www.example.com/foo/foo.htm [R=301,L]
RewriteRule ^old/pages/here/([^.]+)\.php$ http://www.example.com/new/pages/$1.php [R=301,L]
#
# Redirect if correct page URL-path, but requested from wrong (non-canonical) domain
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Jim
Thanks so much for the quick and very informative reply. I do have a couple more questions if you don't mind.
How would I write the rewrite rule to direct a page like this:
/metal/sale6.html to http://www.example.com/sale.html
Would this be the proper syntax:
RewriteRule ^/metal/sale6.html$ http://www.example.com/metal/sale.html [R=301,L]
Also, how would I write the rule to redirect everything in specific sub directory to a certain page in another subdirectory:
Example:
Everything inside the pewter subdirectory /pewter/ to go to http://www.example.com/metal/pewter.html
Would it be written somewhat like this:
Rewrite Rule ^/pewter/not sure what goes here$ http://www.example.com/metal/pewter.html [R=301,L]
Thanks again
Kelley
For more information on writing patterns and rules, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim