【发布时间】: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/…)。如果这不是发布此类问题的地方,请告诉我。