微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当目录存在时,htaccessredirect无法redirect

为了制作友好的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,QSA]

这现在适用于:

mydomain.com/pages到mydomain.com/index.PHP?id1=pages

mydomain.com/pages/xyz到mydomain.com/index.PHP?id1=pages&id2=xyz

.htaccess重写友好的URL

我如何301使用.htaccessredirect特定的子域URL?

htaccess如何将子目录redirect到外部URL

IP限制与htaccess

htaccess外部重写/内部redirect

另外,当我在url中手动inputmydomain.com/index.PHP?id1=pages&id2=xyz时,它会redirect到mydomain.com 。

现在,当我进入另一个像mydomain.com/templates模板是存在的目录,它redirect到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避免这种情况(当存在具有该名称的目录时)?

注意:这个问题已经在这里提出。 但再次张贴,因为它没有得到相当多的意见,也没有回答这个问题!

htaccess密码保护,但不在本地主机上

使用.htaccess在Apache上从CakePHP 2.x中删除添加尾部斜杠

问题redirect403禁止404找不到

CodeIgniter删除index.PHP不工作

Apache .htacces重写规则以删除.PHP文件扩展名

您应该使用重定向规则在目录前强制结尾斜杠:

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]

注意:从未测试

RewriteEngine On # If the director's are static # Redirect exclude for templates,also can add the aonther directory's RewriteRule ^(templates|another_directory)($|/) - [L] # Redirect General RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)$ index.PHP?id1=$1&id2=$2 [L] RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/ index.PHP?id1=$1&id2=$2 [L] RewriteRule ^([a-zA-Z0-9-/]+)/$ index.PHP?id1=$1 [L]

结果

mydomain.com/pages/ = mydomain.com/index.PHP?id1=1

mydomain.com/pages/xyz = mydomain.com/index.PHP?id1=pages&id2=xyz

它不会重定向mydomain.com/templates/和mydomain.com/another_directory/

如果/ templates是一个存在的目录,你可以使用RewriteCond来排除它

RewriteCond %{REQUEST_URI} !^/templates [NC] RewriteRule ^([^/.]+)$ index.PHP?id1=$1 [NC,QSA]

或者你可以在你的其他规则之前使用下面的规则来传递uri:

RewriteRule ^templates - [L]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐