Redirect URL with query string htaccess

2019-07-20 17:24发布

问题:

I need to redirect /path/?start=10 to https://www.example.com/new-path/

How can I do this for every URL needed in my .htaccess?

I have tried:

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/

but none of these work.

回答1:

You can not RedirectMatch or Redirect queryString , you need to use RewriteRule for this purpose:

RewriteEngine on

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

An empty question mark at the end of the destination url is important to discard old querystring from the new url. If this is omitted ,mod rewrite will append querystring to the target url and your url will look something like http://example.com/new-path?test=10 .



回答2:

You can use mod alias :

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

Or use mod_rewrite

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