Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Can I create 301 redirect from "index.html" to root directory if file "index.html" not exist?

For example: link "http://example.com/index.html" show "404 Error" page.

I need 301 redirect to root directory: "http://example.com/"

in .htaccess I add rule:

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.html 
RewriteRule ^(.*)index.html$ http://example.com/$1 [R=301,L]

but it doesn't work. Can you help me solve this problem?

share|improve this question

migrated from stackoverflow.com Apr 6 '13 at 14:28

This question came from our site for professional and enthusiast programmers.

3 Answers 3

Use it like this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*?/|)index\.html$ / [L,R=301,NC]
share|improve this answer

Try to check this one:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/index.html$
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*) http://%{HTTP_HOST}/ [R=301]

I'm sorry, for I forgot something. It's now edited!


Try this second one!

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/index.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) http://%{HTTP_HOST}/ [R=301]
share|improve this answer
    
Doesnt work, maybe you know reason for this? –  Andrij Muzychka Apr 3 '13 at 10:55
    
Have you ever tried to run the code by itself? Now, check the updates' new code if it will going to work. –  Servant Apr 3 '13 at 11:07

If Request Filename ends with index.html AND if index.html does not exists, then redirect it to http://example.com

Try below rewrite rule:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^(.*)/index.html$
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)/index.html$ http://example.com/$1 [R=301,L,NC]

Above rule works for me

share|improve this answer
    
Doesn't work, this rule show 310 redirect on all pages. (I mean firs rule) 2nd rule don't change anything –  Andrij Muzychka Apr 3 '13 at 10:20
    
Try this one RewriteEngine On RewriteCond %{REQUEST_FILENAME} ^(.*)/index.html$ RewriteCond %{REQUEST_FILENAME} -s RewriteRule ^.*$ - [NC,L] RewriteRule ^(.*)/index.html$ /test.php?a=$1 [R=301,L,NC] –  Amit Apr 3 '13 at 10:31
    
RewriteRule is not properly visible in comments, so updated by answer –  Amit Apr 3 '13 at 10:34
    
The same 404error Thanks for try, maybe you know where can be this problem? –  Andrij Muzychka Apr 3 '13 at 10:51
    
can you check Firebug -> Net? Is RewriteRule is giving 404 or it gives 404 after redirecting to example.com/$1 –  Amit Apr 3 '13 at 10:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.