使用查询字符串htaccess重定向URL

时间:2017-03-17 16:24:40

标签: apache .htaccess redirect url-rewriting

我需要将/ path /?start = 10重定向到https://www.example.com/new-path/

如何为.htaccess中所需的每个网址执行此操作?

我试过了:

redirectMatch 301 ^/path/?start=10 https://www.example.com/new-path/

RewriteRule ^/path/?start=10$ https://www.example.com/new-path/

Redirect 301 /path/?start=10 https://www.example.com/new-path/

但这些都不起作用。

2 个答案:

答案 0 :(得分:2)

您无法 RedirectMatch 重定向 queryString,您需要使用 RewriteRule 来实现此目的:

RewriteEngine on

RewriteCond %{THE_REQUEST} /path/\?start=10
RewriteRule ^ https://example.com/new-path? [L,R]

目标网址末尾的空问号对于从新网址中丢弃旧的查询字符串非常重要。如果省略,则mod重写会将查询字符串附加到目标网址,您的网址将类似于 http://example.com/new-path?test=10

答案 1 :(得分:0)

您可以使用mod别名:

Redirect 301 /path/?start=10 /new-path/

或使用mod_rewrite

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^/path/?start=10$ /new-path/$1 [L,R=301]