为了制作友好的 URL,以下是应用的 htaccess 代码:
RewriteEngine On
RewriteCond %{THE_REQUEST} /.+?\.php[\s?] [NC]
RewriteRule ^ - [F]
RewriteRule ^([^/\.]+)$ index.php?id1=$1 [NC,L,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?id1=$1&id2=$2 [NC,L,QSA]
这现在适用于:
mydomain.com/pages
to mydomain.com/index.php?id1=pages
mydomain.com/pages/xyz
to mydomain.com/index.php?id1=pages&id2=xyz
另外,当我进入mydomain.com/index.php?id1=pages&id2=xyz
手动输入 URL,它会重定向到mydomain.com
.
现在,当我输入另一个像mydomain.com/templates
其中 templates 是一个存在的目录,它重定向到mydomain.com/templates/?id1=templates
我尝试添加这些行但徒劳:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
我的目录结构:
www/
.htaccess
index.php <--- main router
templates/
img/
css/
...
...
other_directories/
...
...
index.php
应该收到 id1 和 id2。
我该如何使用 htaccess 避免这种情况(当存在具有该名称的目录时)?
Note:这个问题已经被问过here https://stackoverflow.com/questions/36835920/htaccess-redirect-when-directory-exists。但再次发帖,因为它没有获得足够的浏览量,也没有回答问题!