为什么我的 RewriteRules 不能很好地协同工作?
我的代码的第一部分起作用并重定向/重写到 new/file 。当我尝试传递变量时出现问题。它们都被重定向到 new/file
无论我尝试在 file.php?foo=bar
中传递什么变量,都会重定向到 new/file
而不是新建/文件/栏
。
例如:
- new/file 重写为 file.php
- new/file/2 重写为 file.php?page=2
- file.php 重定向到 new/file
- file.php?page=2 不重定向到 new/file/2但 get 被覆盖为 new/file
我的代码:
RewriteBase /domain.com
#regular
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file? [NS,R=301,L]
RewriteRule ^new/file?$ file.php?redirect=no [NS]
#with variable
RewriteCond %{QUERY_STRING} ^page=([0-9-]+)/?$
RewriteRule ^file\.php$ new/file/%1? [NS,R=301,L]
RewriteRule ^new/file/([0-9-]+)/?$ file.php?page=$1&redirect=no [NS]
请注意,我传递的不仅仅是 page
变量。
我在想也许QSA 标志应该放在某个地方?
The first part of my code works and redirects/rewrites to new/file
. Problem occurs when I try to pass variables. They all get redirected to new/file
Whatever variable I try to pass in file.php?foo=bar
redirects to new/file
instead of new/file/bar
.
for example:
- new/file rewrites to file.php
- new/file/2 rewrites to file.php?page=2
- file.php redirects to new/file
- file.php?page=2 doesn't redirect to new/file/2 but get's overwritten to new/file instead
My code:
RewriteBase /domain.com
#regular
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file? [NS,R=301,L]
RewriteRule ^new/file?$ file.php?redirect=no [NS]
#with variable
RewriteCond %{QUERY_STRING} ^page=([0-9-]+)/?$
RewriteRule ^file\.php$ new/file/%1? [NS,R=301,L]
RewriteRule ^new/file/([0-9-]+)/?$ file.php?page=$1&redirect=no [NS]
Please note that I am passing more than just page
variables.
I'm thinking maybe a QSA flag is supposed to go somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第 2 行你有 ?.这将替换当前的查询字符串,并且 page=2 会丢失。
在第 3 行中,您有一个 ?。路径不包含问号。其次,问号被解释为“可选字母'e'”,因为它是一个正则表达式。所以“/new/fil”也可以工作(尝试一下)。
In line 2 you had ?. This will replace the current querystring, and page=2 is lost.
In line 3 you had a ?. Paths don't contain questionmarks. Secondly the questionmark is interpreted as an "optional letter 'e'", because it is a RegEx. So "/new/fil" would also work (try it out).