简体   繁体   中英

Redirect a URL with querystring to to a new URL with same querystring

I currently have the following in my .htaccess file that redirects all requests to the SSL version of the site:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*) https://www.mydomain.com$1 [R=301,nc]

Now, I'd like to redirect calls to my current root URL - only when it has a querystring attached - to a new url with the same querystring. Example:

https://www.mydomain.com/?foo=bar redirected to:

https://www.mydomain.com/new_mapping/?foo=bar

Whereas requests without a querystring continue to go to https://www.mydomain.com/

Thanks in advance for any help.

Use a RewriteCond to match a nonempty query string with .+ and rewrite it with [QSA]

RewriteEngine On
# If the querystring has at least 1 character
RewriteCond %{QUERY_STRING} ^.+
# Rewrite requests to the root 
RewriteRule ^/?$ new_mapping [L,QSA]

If you want to actually redirect the browser to show the new URL, use [R=301] . The [QSA] is technically not necessary then, because the query string will automatically be appended for a redirect.

RewriteRule ^/?$ new_mapping [L,R=301,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM