【问题标题】:How to set up rewrite rule for a list of keywords in the URL?如何为 URL 中的关键字列表设置重写规则?
【发布时间】:2020-04-05 01:46:46
【问题描述】:

我想做的事

我有许多需要重定向的 URL,以及被发送到浏览器的 301 永久重定向标头。我已经确定在 htaccess 级别执行此操作是最有效的(而不是使用与此相关的 Wordpress 站点中的函数执行此操作)。

要重定向的 URL 是:

https://www.mydomain.com.au/search-result/?location=victoria

https://www.mydomain.com.au/search-result/?location=new-south-wales

https://www.mydomain.com.au/search-result/?location=queensland

https://www.mydomain.com.au/search-result/?location=south-australia

https://www.mydomain.com.au/search-result/?location=tasmania

https://www.mydomain.com.au/search-result/?location=northern-territory

重定向到哪里

我想将它们重定向到主页:https://mydomain.com.au/(我以后可能会选择将它们全部重定向到其他地方,但我可以做到这一点)。

注意:查询字符串应从重定向中删除。

我不确定是最好测试所有六个location= 变量,还是只测试一个不重定向的location= 变量。

不重定向的location= 变量是?location=western-australia。例如,

https://www.mydomain.com.au/search-result/?location=western-australia

其他注意事项

请注意,还有其他.../search-result/ URL 在查询字符串中具有不同的变量,例如?weather=...?water=...。例如https://www.mydomain.com.au/search-result/?location=victoria&weather=part-shade&water=&pasture=

如该示例所示,查询字符串中也可能包含多个变量,例如?location=tasmania&weather=&water=moderate&pasture=

所以我需要测试上面列出的location= 是否存在,无论它后面是否有其他变量。 location= 变量始终是整个查询字符串中的第一个。

我认为这可能就像测试 URL 中是否存在 /search-result/ 和后跟 victoria | tasmania | northern-territory | etc. 一样简单。我不能 100% 确定这些词(victoria 等)不会出现在任何其他 URL 中,因此我只在这些词跟随 location=/search-result/ 时才进行重定向。我怀疑location= 会是一个合适的条件。

我已经修改了许多我在网上找到的重写规则示例,但没有任何效果。我要么收到 501 错误(网站崩溃),要么什么都不会发生。

谢谢。

【问题讨论】:

  • 我不确定为什么有人将其选为“离题”。在决定将其发布到何处时,在 Google 上进行的搜索显示 stackoverflow htaccess rewriterule 的 159,000 个结果(参见此处:google.com/…)。如果这不是发布此类问题的地方,请告诉我。

标签: regex .htaccess redirect


【解决方案1】:

不确定您是否尝试过这些,但它们对我很有效:

  • 允许任何location 值,western-australia 除外:

    # The request path is /search-result/ or maybe /search-result
    RewriteCond %{REQUEST_URI} ^/search-result/?$
    
    # ..and the query string 'location' is not empty
    RewriteCond %{QUERY_STRING} (^|&)location=.+($|&)
    
    # ..and the value is not 'western-australia'.
    RewriteCond %{QUERY_STRING} !(^|&)location=western-australia($|&)
    
    # Redirect to the home page.
    RewriteRule . / [R=301,NC,L]
    
  • 只允许某些location 值:

    RewriteCond %{REQUEST_URI} ^/search-result/?$
    
    // Allow only certain location values - (<value>|<value>|...).
    RewriteCond %{QUERY_STRING} (^|&)location=(victoria|new-south-wales)($|&)
    
    RewriteRule . / [R=301,NC,L]
    

请注意,在 WordPress 中,您需要将上述 放在 WordPress 规则之前:

# This is a sample .htaccess file used on a WordPress site.

# PLACE YOUR CUSTOM RULES HERE.

# BEGIN WordPress
<IfModule mod_rewrite.c>
    # ...
    RewriteRule . /index.php [L]
</IfModule>
# END WordPress

即将您的规则放在# BEGIN WordPress 行上方,以避免出现404 错误。

顺便说一句,我不是htaccess 专家,但希望这个答案对您有所帮助。 :)

【讨论】:

  • 谢谢。我尝试了您给出的第二个示例(“仅允许某些位置值:”),它产生了奇怪的效果。以tasmania 为例,如果该位置在列表中,它最终会重定向到:https://www.mydomain.com.au/location/tasmania。如果它不在列表中,则不会发生任何事情(如预期的那样)。是否有可能在 htaccess 重定向之后,Wordpress 会以某种方式进行第二次重定向?
  • 这里的分析 (redirect-checker.org/index.php) 表明第一个重定向重定向到 mydomain.com.au/?location=tasmania。所以它保留了查询字符串。如何完全消除查询字符串?
  • 我在上面通过将重写规则更改为RewriteRule .* /? [R=301,NC,L]来解决上述问题,这是我在这里找到的建议:electrictoolbox.com/rewrite-rule-redirect-without-query-string
  • 我发现的另一个解决方案是添加查询字符串丢弃 [QSD] 标志。例如,RewriteRule . / [R=301,NC,L,QSD],在此处详述,httpd.apache.org/docs/trunk/rewrite/flags.html#flag_qsd。 - 因为那更干净,这就是我要运行的。感谢您的帮助。
  • 我猜你服务器的默认RewriteRule标志是QSA?因为在我的例子中,现有的查询字符串没有被添加到重定向 URL 中。
猜你喜欢
  • 2017-01-02
  • 2013-06-08
  • 2016-10-17
  • 2021-06-26
  • 2012-11-05
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 2013-04-18
相关资源
最近更新 更多