homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

301 Redirect + RedirectMatch
301 Redirect but cant shift /index.html

 10:33 am on Dec 11, 2007 (gmt 0)

This one has been flogged so many times Im sorry to have to raise the query again.

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)


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

[edited by: jdMorgan at 2:24 pm (utc) on Dec. 11, 2007]
[edit reason] example.com [/edit]



 2:21 pm on Dec 11, 2007 (gmt 0)

The code to redirect index.html (or index.php) to "/" has been posted many times here. Try a WebmasterWorld search [google.com] to turn up these threads. Some examples redirect only the root index page to "/", while others will redirect index pages in subdirectories to "/" within those subdirectories. Choose whichever method you need.

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.



 2:29 pm on Dec 11, 2007 (gmt 0)

I missed a couple of points:

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 "/".


[edited by: jdMorgan at 2:29 pm (utc) on Dec. 11, 2007]


 6:47 pm on Dec 11, 2007 (gmt 0)

Good of you to chime in

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




 7:04 pm on Dec 11, 2007 (gmt 0)

If you leave that unconditional RedirectMatch in-place, you'll likely have a rankings disaster -- As I said, it will result in an 'infinite' redirection loop -- and I doubt that your visitors or the search engines will appreciate that very much...

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]

Place this ahead of your current non-www to www redirect rule, after the "RewriteEngine on" line.



 8:41 pm on Dec 11, 2007 (gmt 0)

So I replace the current

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


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]


You dont use both?

Dont get the ^[A-Z} bit either but darent push my luck on that one!


 8:54 pm on Dec 11, 2007 (gmt 0)

Just tested using BOTH together and it works a treat!

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?


 9:02 pm on Dec 11, 2007 (gmt 0)

Yes, but you might want to add some coments to document the function of each rule, in case you need to go back to them in the future... Also the "^" and "$" are not needed in the second rule's pattern, and you can remove them to save your CPU a small bit of wasted effort.



 9:07 pm on Dec 11, 2007 (gmt 0)

Many thanks jd
Your a star!


 1:27 am on Dec 13, 2007 (gmt 0)

In my version of that code I add a question mark directly after the $1 in both pieces of code.

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]


 1:34 am on Dec 13, 2007 (gmt 0)

Check that second RewriteCond pattern -- the two spaces need to be escaped:

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]

Change all broken pipe "¦" characters above to solid pipe characters before use; Posting on this forum modifies the pipe characters.


[edited by: jdMorgan at 1:35 am (utc) on Dec. 13, 2007]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved