domain.com/index.html to domain.com using htaccess

Discussion in 'Apache' started by Sorror, Nov 15, 2008.

  1. #1
    Simple question about htaccess rule: how can I redirect all queries like "domain.com/index.html" to "domain.com" so the index.html wouldn't be seen on address bar in web browser (or in google, etc)?
     
    Sorror, Nov 15, 2008 IP
  2. diligenthost

    diligenthost Peon

    Messages:
    685
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Does it automatically go to /index.html when just going to the site by typing in domain.com? If so, this will cause a loop and won't work ;)
     
    diligenthost, Nov 15, 2008 IP
  3. Sorror

    Sorror Active Member

    Messages:
    376
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #3
    No it doesn't, so there's no fear of a loop :) Anyway, I've find out how to achieve it just after wrote this post, so:

    
    RewriteCond %{the_request} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
    RewriteRule ^(.*)index\.html$ http://domain\.com/$1 [l,r=301]
    
    Code (markup):
     
    Sorror, Nov 15, 2008 IP
  4. joncom11

    joncom11 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I was having the same problem. I wanted every case of /index.[html|htm|php] to always redirect to just the [/] directory folder that holds the index file, whatever it may be.

    I accomplished this task by changing around Sorror's suggested code a bit. This works great for me on my server:
    RewriteEngine on
    RewriteCond %{the_request} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
    RewriteRule ^(.*)index\.html$ http://www\.mydomain\.com/$1 [R=301,L]
    RewriteCond %{the_request} ^[A-Z]{3,9}\ /.*index\.htm\ HTTP/
    RewriteRule ^(.*)index\.htm$ http://www\.mydomain\.com/$1 [R=301,L]
    RewriteCond %{the_request} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
    RewriteRule ^(.*)index\.php$ http://www\.mydomain\.com/$1 [R=301,L]
    Code (markup):
     
    joncom11, Nov 15, 2008 IP