Forum Moderators: phranque
As usual for the programatically challenged I am struggling to find a thread that is on all fours with my problem. Without such a thread Im stumbling about and dont want to cause any damage to my sites.
After the usual canonical problems with a few of my sites I have added 301 redirects with the following syntax
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
However this doesn't help re-direct any /index.html pages and unfortunately on one site all internal pages point to this file.
So my key questions are:
Do I use the following to complete the re-direct:
RedirectMatch 301 ^/index.html$ /
If so where do I paste this into the above main .htaccess code? (dont want to create any conflicts or loops)
Also
What should I do re the internal pages that all point to /index.html? Just change them all physically to root/ or is this not required once the RedirectMatch?
Thanks in advance
C
[edited by: jdMorgan at 2:24 pm (utc) on Dec. 11, 2007]
[edit reason] example.com [/edit]
The index redirect code should be placed above the domain canonicalization rule you posted, following the method "Most-specific external redirect first, less-specific external redirects after, most-specific internal rewrites next, least-specific internal rewrite last.
Jim
You cannot use Redirect or RedirectMatch to redirect index.html to "/" -- Attempting to do so will cause an 'infinite' redirection loop.
The purpose of the redirect is to 'correct' search engines listings and type-ins so that "/index.html" will no longer be linked-to or listed in search results. You should also correct all links within your site to point to "/" instead of "/index.html". Failure to do so means that your visitors will suffer the delay of their browsers' always having to request the index page content twice, and you also depend on search engines to follow the redirect and properly 'carry over' the PageRank or Link popularity of "index.html" to "/".
Jim
[edited by: jdMorgan at 2:29 pm (utc) on Dec. 11, 2007]
I know its a subject thats been covered often on these boards. I did a quite lengthy search before posting but could not find what I wanted - in simple enough terms
You seem to be saying that I should just leave the RedirectMatch and let nature take its course re the main .htaccess command. Is that the way to go?
If I change all internal pages at the same time to point to the correct root then that resolves the on site issues I guess
Just worried that the index page stays out there and continues to split the PR
Regds
C
Here's an example of the correct (non-looping) method using mod_rewrite, copied directly from the very first result of the search link I posted above:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
Jim
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
with
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
Right?
You dont use both?
Dont get the ^[A-Z} bit either but darent push my luck on that one!
Can I rest easy now and assume that Ive got this right with the full .htaccess reading as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
What do you reckon Jd?
That has the effect of clearing any query string that was present in the initial request.
.
In the index file redirect, I redirect for all of the common and well-known index file filenames:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^/]*/)*(index¦default¦home)\.(html?¦php(4¦5)?¦aspx?¦cfm).* HTTP/ [NC]
RewriteRule ^(([^/]*/)*)(index¦default¦home)\.(html?¦php(4¦5)?¦aspx?¦cfm)$ ht tp://www.example.org/$1? [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*(index¦default¦home)\.(html?¦php(4¦5)?¦aspx?¦cfm)[^\ ]*\ HTTP/ [NC]
RewriteRule ^(([^/]*/)*)(index¦default¦home)\.(html?¦php(4¦5)?¦aspx?¦cfm)$ http://www.example.org/$1? [NC,R=301,L]
Jim
[edited by: jdMorgan at 1:35 am (utc) on Dec. 13, 2007]