友情URL与子文件夹index.html冲突

时间:2014-07-20 12:45:24

标签: regex apache .htaccess mod-rewrite

我目前有一个包含这些mod_rewrite规则的.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>`

它适用于我的主项目的友好URL,但是当我尝试通过URL /子文件夹/或/子文件夹访问/subfolder/index.html时,我发生冲突,它会循环。

我尝试过很多事情没有成功,我想我必须使用RewriteCond %{REQUEST_FILENAME} -d然后将RewriteRule设为index.html,但是当我成功时,那么网址/以许可被拒绝错误。

如何让子文件夹转到它自己的index.html并且仍然有友好的URL?

澄清说明:友好网址不会也不会与现有文件夹匹配

1 个答案:

答案 0 :(得分:2)

您可以同时使用DirectoryIndexRewriteCond %{REQUEST_FILENAME} -d

DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [L,R=301,NE]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule