Pradeep Sathyaraj
Pradeep Sathyaraj

Reputation: 134

.htaccess redirect all index.html to parent folder

I need to redirect all index.html like this

Original URL

  1. www.example.com/lp/index.html
  2. www.example.com/sytem/index.html

Desired URL

  1. www.example.com/lp
  2. www.example.com/sytem

I used the follwing, it redirects successfully but 404 page

RewriteEngine On

  RewriteRule ^index\.html$  [R=301,L]
  RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

Upvotes: 2

Views: 3755

Answers (4)

Sidupac
Sidupac

Reputation: 671

why not redirect index.(anything) ? like index.php? index.xhtml?

RewriteEngine On
RewriteRule ^index\.(.*)$ / [R=301,L]
RewriteRule ^(.*)/index\.(.*)$ /$1/ [R=301,L]

or to base it from the initial URL request only (to avoid conflicts with other rewrite rules):

RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.(.*) [NC]
RewriteRule ^(.*?)index\.(.*)$ /$1 [R=301,L]

Upvotes: 0

anubhava
anubhava

Reputation: 785306

Place this code in your DOCUMENT_ROOT/.htaccess file:

DirectoryIndex index.html
RewriteEngine On

RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*?)index\.html$ /$1 [L,R=301,NC,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1/index.html [L]

Upvotes: 1

manei_cc
manei_cc

Reputation: 175

Try putting this in your .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1/index\.html [PT,L]

PT and L are rewrite flags, for more details about them check http://httpd.apache.org/docs/2.2/rewrite/flags.html

Upvotes: 0

meberhard
meberhard

Reputation: 1845

You almost got it right, what you did looks like a copy paste error to me. Your first RewriteRule is missing a parameter - there should be a slash before [R=301,L]

Your htaccess should look like this:

RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

Reference: http://dense13.com/blog/2012/10/29/removing-index-html-with-mod_rewrite-in-htaccess/

Upvotes: 2

Related Questions