SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)

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

    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]
    Curently I'm using this code to rewrite the index.html to www.mydomain.com/...

    1)
    Code:
    # 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.

    2)
    Code:
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
    RewriteRule ^(.*)index\.html$ http://www.example.com/$1 [R=301,L]
    A little cleaner it seems but does it work the same?

    3)
    Code:
    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

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

  2. #2
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Nobody knows?

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    EW,

    Redirecting to remove the DirectoryIndex? Horrors!

    Oh, well, let me go over your code:

    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:

    Code:
    RewriteRule .? http://www.mydomain.com%{REQUEST_URI} [R=301,L]
    Code:
    # 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.

    Code:
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
    RewriteRule ^(.*)index\.html$ http://www.example.com/$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.

    Code:
    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.

    Code:
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Here is how to do what I originally asked http://www.visibilityinherit.com/cod...-to-folder.php


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •