【问题标题】:How do you redirect URLs with a query string to another page?如何将带有查询字符串的 URL 重定向到另一个页面?
【发布时间】:2018-10-25 23:13:56
【问题描述】:

这个 htaccess sn-p 应该重定向

mywebsite.com/product-category/clothing/?orderby=popularity

mywebsite.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mywebsite.com.com$ [NC]
RewriteRule ^/\?p=([0-9]+)&preview=true$ http://alt.myhost.com/?p=$1&preview=true [NC,R=301,L]

但由于某种原因,我无法逃脱 / 和 ? URL 的一部分。不知道为什么这不起作用...

我试过逃跑吗?

\\? \? [?]

我已经尝试转义 /

\\/ \/ [/]

这些似乎都不起作用......

帮助!

【问题讨论】:

    标签: .htaccess redirect query-string url-redirection http-redirect


    【解决方案1】:

    url 中? 之后的任何内容都是 URL QueryString 的一部分。

    您不能以 RewriteRule 的模式测试 url QueryString。您需要在RewriteCond 指令中匹配%{QUERY_STRING} 变量,如下所示:

    RewriteEngine on
    
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^mywebsite.com.com$ [NC]
    RewriteCond %{QUERY_STRING} ^orderby=popularty$ [NC]
    RewriteRule ^product-category/clothing/?$ http://example.com/?  [NC,R=301,L]
    

    这会将 http://mywebsite.com/product-category/clothing/?orderby=popularty 重定向到 http://example.com/

    目标 url 末尾的空问号 (http://example.com/?) 很重要,因为它会丢弃目标 path/url 中的旧查询字符串。

    【讨论】:

    • 如何将 mywebsite.com/?orderby=price 重定向到 ?orderby=price @starkeen
    • /?orderby=price/?orderby=price 不是相似的 uris?
    • 嘿抱歉,这是拼写错误,如何将 mywebsite.com/?orderby=price 重定向到 mywebsite.com @starkeen
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2013-01-10
    相关资源
    最近更新 更多