1

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.

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

That doesn't seem to be doing anything. Should I place it in a certain place in relation to my current RewriteRule?
@AbidA It should be placed after your SSL redirect. I'll also change it to `^/?$ though that should not be necessary. To debug, remove the RewriteCond and verify that the root URL rule is matched, then add back the RewriteCond.
Now I'm getting a 400 Bad request. Everything seems to work fine, except when the querystring is applied to the root URL.
Oh, so I added the complete URL https://www.mydomain.com/new_mapping and that worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.