如何区分文档请求和变量请求?以及如何允许不需要的网址?
我的上一篇文章得到了狡猾、无益的“答案”(评论),所以我会直接说:
如果我有一个像这样的 htaccess 文件:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&subject=$2
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
我怎样才能允许处理其他 url 变量名称和值。举例来说,我想在这种情况下添加额外的意外网址变量
/page/subject?urlvar1=value1&urlvar2=value2
并获得我想要的页面而不产生意外结果?
非常感谢任何真正的帮助。谢谢!
My last post was met by smarmy, unhelpful "answers" (comments), so i'll get right to it:
if I have an htaccess file like so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&subject=$2
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
how can I allow for other url variable names and values to be handled... say for instance I want to add extra unexpected url vars to this scenario
/page/subject?urlvar1=value1&urlvar2=value2
and get the page I want without creating unexpected results?
Any real help is greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将 [QSA](查询字符串附加)标志添加到您的规则中。
作为一般建议,最好只有一个通用规则,例如
在 php 级别解析路径。这更加灵活和透明。
详细说明一下,这是完整的设置
.htaccess
index.php
当像
www.example.com/foo/bar/baz?quux=123&blah=456
这样调用时,它打印出唯一剩下的就是使用爆炸或正则表达式解析 $_GET['path']。
you need to add [QSA] (query string append) flag to your rules.
As a general advice, it's better to have only one generic rule like
and parse the path at php level. This is much more flexible and transparent.
to elaborate, here's the complete setup
.htaccess
index.php
when called like
www.example.com/foo/bar/baz?quux=123&blah=456
this printsthe only thing that's left is to parse $_GET['path'] with explode or regular expressions.