How to redirect /directory/index.html and /directory/index.php to /directory/

五迷三道 提交于 2020-01-05 12:49:07

问题


I have to do some redirects - index.html and index.php to /.

I could redirect www.domain.com/index.html and www.domain.com/index.php to www.domain.com with this line:

RewriteRule ^index\.(php|html?)$ http://www.mydomain.com/ [R=301,L]

However I still have problems with pages like these:

  • www.domain.com/directory/index.html - returns 200 OK
  • www.domain.com/directory/index.php - returns 300 multiple choices (don't know why)

Is it possible to redirect them to www.domain.com/directory/ and do it all with one rule one?

If this helps, here's what's in my .htaccess file now:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

## Redirecd index.html & php to /
RewriteRule ^index\.(php|html?)$ http://www...mydomain...com/ [R=301,L]

## 301 Redirect from non-www to www version
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

## Return 404 status code and display 404.html
ErrorDocument 404 /404.html

</IfModule>

回答1:


Try this code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

## Redirecd index.html & php to /
RewriteCond %{THE_REQUEST} /index\.(html|php) [NC]
RewriteRule ^(.*?)index\.(?:html|php)$ /$1 [L,R=302,NC,NE]

## 301 Redirect from non-www to www version
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

## Return 404 status code and display 404.html
ErrorDocument 404 /404.html

</IfModule>



回答2:


you can try replacing your first rewriterule with following

RewriteRule ^/?(.+/)?index\.(php|html?)$ /$1 [R=301,L]



回答3:


Please try this. This works fine with any directory.

RewriteCond %{THE_REQUEST} ^.*/index\.(html|php)

RewriteRule ^(.*)index\.(html|php)$ http://www.domain.com/$1 [R=301,L]


来源:https://stackoverflow.com/questions/23590452/how-to-redirect-directory-index-html-and-directory-index-php-to-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!