问题
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 OKwww.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