htaccess: how to redirect index.html to www?

1 replies
  • WEB DESIGN
  • |
Here's first part of my htaccess code:

Code:
Options +FollowSymlinks
RewriteEngine on
#redirect non www to www
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,NC]

#redirect index.html to domain name
RewriteRule ^index.html$ / [R=301,L]
RewriteRule ^(.*)/index.html$ /$1/ [R=301,L]

#add trailing slash to all pages
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

Non www to www
The first part successfully redirects from domain.com to www.domain.com/.

index.html to www.domain.com
The second part only redirects www.domain.com/index.html to www.domain.com/

If I test with domain.com/index.html (no www) then it fails and redirects to http://domain.com/http://www.domain.com/ throwing up a 404.

Trailing Slash
Adding a trailing slash successfully changes domain.com to www.domain.com/ - but www.domain.com stays the same.

Anyone know where the errors are in the code?
#htaccess #indexhtml #redirecting #www
  • Profile picture of the author beelzebomb
    Ok, I've answered my own problems in the main - though I've had to remove the 'add trailing slash' code to get everything to work - now I have this:

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    
    #redirect index.html to domain www
    RewriteRule ^index.html$ http://www.domain.com/ [R=301,L]
    
    #redirect non www to www
    RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,NC]
    But nowhere can I find reliable code that will successfully change www.domain.com to www.domain.com/
    {{ DiscussionBoard.errors[9565143].message }}

Trending Topics