htaccess的重定向失败时,目录中重定向 [英] htaccess redirect fails to redirect when directory exists
问题描述
有关制作友好的URL,以下是应用htaccess的code:
For making friendly URLs, Following is the htaccess code applied:
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
到mydomain.com/index.php?id1=pages
mydomain.com/pages/xyz
到 mydomain.com/index.php?id1=pages&id2=xyz
此外,当我进入 mydomain.com/index.php?id1=pages&id2=xyz
在URL中手动,它重定向到 mydomain.com
。
also, when I enter mydomain.com/index.php?id1=pages&id2=xyz
manually in the URL, it redirects to mydomain.com
.
现在,当我进入另一个像 mydomain.com/templates
,其中模板是存在的目录,它重定向到 mydomain.com/templates /?ID1 =模板
Now, when I enter another like mydomain.com/templates
where templates is a directory that exists, it redirects to mydomain.com/templates/?id1=templates
我试着加上几行,却徒劳无功:
I tried adding these lines but in vain:
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。
index.php
should receive id1 and id2.
我该如何避免这种情况(与该名称目录存在)使用htaccess的?
How shall I avoid this condition (when directory with that name exists) using htaccess?
注:这个问题已经在这里问 。但再次发布,因为它没有获得可观的意见,也没有回答这个问题!的
Note: This question is already asked here. But posting again, as it didn't gain considerable views nor answered the issue!
推荐答案
您应该强制尾随斜线在使用重定向规则目录的前面:
You should force a trailing slash in front of directories using a redirect rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /.+?\.php[\s?] [NC]
RewriteRule ^ - [F]
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteRule ^([^/.]+)/?$ index.php?id1=$1 [L,QSA]
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?id1=$1&id2=$2 [L,QSA]
这篇关于htaccess的重定向失败时,目录中重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!