带变量的简单 301 重定向不起作用,为什么?
这是我到目前为止所得到的。第一部分有效,但重定向本身无效。
我需要做什么才能让它发挥作用?
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ page.php?name=$1 [L]
RewriteRule ^page.php?name=([^/\.]+)/?$ /$1 [R=301,L]
另外,如果我有多个这些规则,我是否只将 [L]
保留在最后一个规则上?
Here's what I got so far. The first part works but not the redirect itself.
What do I need to do to make it work?
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ page.php?name=$1 [L]
RewriteRule ^page.php?name=([^/\.]+)/?$ /$1 [R=301,L]
Also if I have multiple of these rules do I leave the [L]
only on the last one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了第一个规则覆盖第二个规则之外,第二个规则也不起作用,因为您试图匹配 RewriteRule 中的查询字符串。尝试这样的事情:(
我包括了
QSA
标志,这样像/foobar?foo=bar
这样的 URL 将被重写为/page.php?name=foobar&foo=bar
而不仅仅是/page.php?name=foobar
。如果您不想这样做,请将其忽略。)注意: 第二个
RewriteCond
就在那里。防止第一个规则在第二个规则匹配后再次匹配。问题是,在 .htaccess 上下文中,mod_rewrite 的行为或多或少就像所有规则都具有PT
标志,导致规则集在每次重写后从头开始重新运行,甚至是内部重写。或者,引用文档:我使用的解决方法是在内部重写触发时使用
E=LOOP:1
设置自定义环境变量,并在执行外部重写之前检查它。请注意,当请求处理在内部重写后重新启动时,Apache 会将REDIRECT_
添加到上一次传递期间设置的所有环境变量的名称中,因此即使我们设置的变量仅命名为LOOP
,我们需要检查的是REDIRECT_LOOP
。Besides the first rule overriding the second one, your second rule also won't work because you're trying to match the query string in a RewriteRule. Try something like this instead:
(I included the
QSA
flag so that an URL like/foobar?foo=bar
will be rewritten to/page.php?name=foobar&foo=bar
instead of just/page.php?name=foobar
. If you don't want that, leave it out.)Note: The second
RewriteCond
is there to keep the first rule from matching again after the second one has matched. The problem is that, in .htaccess context, mod_rewrite acts more or less as if all rules had thePT
flag, causing the ruleset to be rerun from the start after every rewrite, even internal ones. Or, to quote the documentation:The workaround I'm using is to set a custom environment variable with
E=LOOP:1
when the internal rewrite triggers, and check for it before doing the external rewrite. Note that, when the request processing restarts after the internal rewrite, Apache prependsREDIRECT_
to the names of all environment variables set during the previous pass, so even though the variable we set is named justLOOP
, the one we need to check for isREDIRECT_LOOP
.