【问题标题】:RewriteCond %{QUERY_STRING} in mod_rewrite (dynamic to static URL) not workingmod_rewrite(动态到静态 URL)中的 RewriteCond %{QUERY_STRING} 不起作用
【发布时间】:2012-07-17 16:32:30
【问题描述】:

因为我很幸运地设法让查询字符串正确重定向,之前只传递查询字符串参数,并且整个网站和 webmasterworld 中关于查询字符串重定向的总体建议似乎是“将其作为 RewriteCond 查询字符串处理”,我正在尝试对一组大约 10 个 URL 使用以下类型规则。

示例网址:

http://www.example.org/training_book.asp?sInstance=1&EventID=139

到目前为止我所拥有的:

RewriteCond %{QUERY_STRING} ^training_book.asp\?sInstance=1&EventID=139  
RewriteRule /clean-landing-url/ [NC,R=301,L]

所以,我想要发生的是

http://www.site.org/training_book.asp?sInstance=1&EventID=139 301> http://www.site.org/clean-landing-url

但是发生的事情是这样的:

http://www.site.org/training_book.asp?sInstance=1&EventID=139 301> http://www.site.org/training_book.asp/?sInstance=1&EventID=139

它在查询字符串之前添加一个正斜杠,然后解析完整的 URL(显然是 404ing。)

我错过了什么?实际 %{QUERY_STRING} 参数是否存在正则表达式问题?

提前致谢!

编辑-

这是我到目前为止的位置。

根据下面@TerryE 的建议,我尝试实施以下规则。

我有一组带有以下参数的网址:

http://www.example.org/training_book.asp?sInstance=1&EventID=139
http://www.example.org/training_book.asp?sInstance=2&EventID=256
http://www.example.org/training_book.asp?sInstance=5&EventID=188

等等

需要重定向到的地方

http://www.example.org/en/clean-landing-url-one
http://www.example.org/en/clean-landing-url-two
http://www.example.org/en/clean-landing-url-three

等等

这是我目前拥有的 htaccess 文件的确切结构,包括目前运行良好的“简单”重定向的完整示例(注意 - http://example.com > http://www.example.com 重定向在 httpd.conf 中强制执行)

#301 match top level pages
RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule ^/faq.asp /en/faqs/ [NC,R=301,L] 

此块中的所有 URL 都属于这种类型。所有这些 URL 都可以完美运行。

#Redirect all old dead PDF links to English homepage.
RewriteRule ^/AR08-09.pdf /en/ [NC,R=301,L]

此块中的所有 URL 都属于这种类型。所有这些 URL 都可以完美运行。

问题出在这里:我仍然无法将以下类型的 URL 重定向。根据@TerryE 的建议,我尝试如下更改语法。下面的块不能正常工作。

#301 event course pages
RewriteCond %{QUERY_STRING} sInstance=1EventID=139$
RewriteRule ^training_book\.asp$ /en/clean-landing-url-one? [NC,R=301,L]

这个的输出是

http://staging.example.org/training_book.asp/?sInstance=1&EventID=139

(目前适用于staging.example.org,将适用于example.org)

(我已经“隐藏”了一些实际语法,方法是在最初的问题中将它从 training_book 更改为 event_book,但我已将其改回尽可能真实。)

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    文档。 QUERY_STRING 包含在? 之后 的请求内容。您的条件正则表达式不应该匹配。这更有意义:

    RewriteCond %{QUERY_STRING}   ^sInstance=1&EventID=139$ 
    RewriteRule ^event_book\.asp$ /clean-landing-url/ [NC,R=301,L]
    

    正斜杠是由不同的 Apache 过滤器 (DirectorySlash) 引起的。

    【讨论】:

    • 感谢您的输入@TerryE,但这根本没有改变行为。 example.com/news.asp 正确地转到 example.com/en/news,这是一个简单的重定向,效果很好。目前在 new.example.com 上设置了一个虚拟主机以在上线前测试重定向。这是我要重定向的确切 URL,使用示例而不是站点名称:example.org/training_book.asp?sInstance=1&EventID=139 > new.example.com/clean-landing-url 我错过了什么?提前感谢您提供任何进一步的想法。
    • @BobC,从您第一次重定向到 .../en/...,您显然还有其他一些可能会干扰的规则。更新您的 Q 以包含所有规则,以便我们了解发生了什么。
    • 嗨特里 - 我已按要求进行了更新。非常感谢您的帮助
    • 哦,请注意我从第一个 ^sInstance 块中提取了胡萝卜字符,这是基于我从另一个 Stack Overflow 帖子中找到的关于该主题的帖子的 cmets 线程:simonecarletti.com/blog/2009/01/…跨度>
    • 我想通了。不是胡萝卜。 Rewriterule 的第一部分缺少一个斜杠!以上编辑。
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多