[英]How to redirect /directory/index.html and /directory/index.php to /directory/
我必须做一些重定向-index.html和index.php到/。
我可以使用以下行将www.domain.com/index.html
和www.domain.com/index.php
重定向到www.domain.com
:
RewriteRule ^index\.(php|html?)$ http://www.mydomain.com/ [R=301,L]
但是我仍然对以下页面有问题:
www.domain.com/directory/index.html
返回200 OK www.domain.com/directory/index.php
返回300个多项选择 (不知道为什么) 是否可以将它们重定向到www.domain.com/directory/
并通过一条规则完成所有操作?
如果有帮助,这是我的.htaccess文件中的内容:
<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>
试试这个代码:
<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>
您可以尝试使用以下方法替换您的第一个重写器
RewriteRule ^/?(.+/)?index\.(php|html?)$ /$1 [R=301,L]
请尝试这个。 这适用于任何目录。
RewriteCond %{THE_REQUEST} ^.*/index\.(html|php)
RewriteRule ^(.*)index\.(html|php)$ http://www.domain.com/$1 [R=301,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.