Forum Moderators: phranque
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
That's working fine, however the www.../index.html is not redirecting to www.example.com/
Also, when I click the homepage links (index.html) in my internal site pages, it arrives at www.example.com/index.html
All my incoming links from external sites arrive at www.example.com/
Will this cause a duplication penalty in google and should I have the www.example.com/index.html redirected?
[edited by: jdMorgan at 11:17 pm (utc) on Feb. 1, 2009]
[edit reason] example.com [/edit]
That new rule will also need to force www for just those requests so that you don't have a redirection chain.
Code for that has been posted several times in recent weeks in this forum.
For your existing rule you also need to fix the casing of the various parts of the rule as per the Apache docs.
Additionally, you need to link to "/" and to "/folder/" within your site, and not to URLs that include the index file filename.
What about appended FQDN indicators or port numbers? -- www.example.com.:80 is perfectly-valid but non-canonical...
I'd suggest:
Options +FollowSymlinks
RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.(s?html?¦php[456])(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(s?html?¦php[456])$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
The pattern in the RewriteCond and Rewrite rule accepts a request for /index.html, /index.shtm, /index.php, /index.php5, etc. in *any* directory, and the rule redirects the client to "/" in that same directory.
Replace the broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.
In the second rule we require an *exact* match on the hostname or a blank hostname. Accepting a blank hostname prevents putting your server into an infinite loop if you ever get a request from an HTTP/1.0 client, which won't ever send a "host" header.
Jim
[edited by: jdMorgan at 2:08 am (utc) on Feb. 2, 2009]
Run a very tight ship, and you won't find yourself posting in the "sudden mysterious search rankings drop" threads here... :)
Basically, any kind of sloppiness in your linking or your server's handling of requests that "aren't quite right" is an opportunity for another Webmaster to make matters worse for you, whether unintentionally or otherwise. If an "imperfect request" can be unambiguously detected and corrected, then 301-redirect it to the correct URL. If not, a 404-Not Found response is likely most appropriate.
Note to all readers: The solid pipe character mentioned above varies in both presentation and in required keyboard keying based on your browser and operating system character-set and character-encoding. The desired character is the US-ASCII or UTF-8 hexadecimal character-code %7C.
Jim