Canonicalization: RewriteRule index to root in htaccess: which code is better & why?

RewriteRule index.html to root in htaccess: which code is better? And why? What do they do different? Thanks for your thoughts! I’m getting rid of my duplicate pages. Basically which code is cleanest that will do the job and slow the server response down the least. I do wish to use code that takes care of all my index files in all subdirectories. And I only have to say RewriteEngine On once right?

I got rid of the non-www with this. I believe this is clean. If not let me know…

# 301 permanent redirect from non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

Curently I’m using this code to rewrite the index.html to www.mydomain.com/

# rewrite index.html to root
RewriteRule ^index\\.html$ http://www.mydomain.com/ [L,R=301]

But then I’ve seen various different versions of this around the web. I think I am to understand that it takes care of all indexes in all directories but it also has the “RewriteCond” line. Whats that do?? FYI I do have other rewrite code in my htacces to if that matters any.

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

A little cleaner it seems but does it work the same?

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

And now what is {IS_SUBREQ} false? Too many ways to skin a cat here

RewriteCond %{IS_SUBREQ} false
RewriteRule ^index\\.(php|html?)$ http://www.martin-thoma.de/terminplaner/ [R=301,L]

Edit #2 actually has [b] before and after the domain. And before $1. Not sure why the [b]'s were removed. Ahh apparently that doubles for bold.

Nobody knows?

EW,

Redirecting to remove the DirectoryIndex? Horrors! :eek2:

Oh, well, let me go over your code:

# 301 permanent redirect from non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

I don’t like duplicating an existing Apache variable so I would replace the last line with:

RewriteRule .? http://www.mydomain.com%{REQUEST_URI} [R=301,L]
# rewrite index.html to root
RewriteRule ^index\\.html$ http://www.mydomain.com/ [L,R=301]

No problem here except that some servers might force the identification of the DirectoryIndex (which would cause this to loop without the {IS_SUBREQ} flag).

But then I’ve seen various different versions of this around the web. I think I am to understand that it takes care of all indexes in all directories but it also has the “RewriteCond” line. Whats that do?? FYI I do have other rewrite code in my htacces to if that matters any.

No, the start anchor (in .htaccess in the DocumentRoot) will only match the index.html in the DocumentRoot.

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

A little cleaner it seems but does it work the same?

It’s NEVER cleaner to use {THE_REQUEST} because of all the spaces (which you have properly escaped). However, it does do the same thing as above.

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

That code will not work and is likely to generate 500 errors for the syntax error (too many spaces in the RewriteRule).

And now what is {IS_SUBREQ} false? Too many ways to skin a cat here

The {IS_SUBREQ} flag is a handy tool to identify whether a {REQUEST_URI} has already been (internally) redirected.

RewriteCond %{IS_SUBREQ} false
RewriteRule ^index\\.(php|html?)$ http://www.martin-thoma.de/terminplaner/ [R=301,L]

That redirects either index.php or index.html (in the DocumentRoot) to the new URL but ONLY if there had not been an internal redirection already.

Regards,

DK

I’m pretty sure I worked it all out over here (I had to get it done). It works and the guy who helped seems to know his stuff. But I wouldnt know if he didnt. http://www.webmasterworld.com/apache/4508251.htm

Here is how to do what I originally asked http://www.visibilityinherit.com/code/canonicalization-htaccess-mod_rewrite-301-redirect-non-www-to-www-and-index-to-folder.php