0
votes

I am using the mod_rewrite condition/rule as shown below to redirect a url to the www equivalent and ensure https.

RewriteCond %{HTTP_HOST} ^example.com [NC]  
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]

This, for example, converts example.com/foo/bar to https://www.example.com/foo/bar and works fine. However, if the link includes a query string (eg, http://example.com/foo/bar?x=baz&y=qux), the query string does not get appended. How can I modify my RewriteCond/RewriteRule above so that http://example.com/foo/bar?x=baz&y=qux is automatically converted to https://www.example.com/foo/bar?x=baz&y=qux with the query string appended? I have tried adding the QSA (Query String Append) flag in the rewrite rule and that doesn't help.

1

1 Answers

0
votes

You need the QSA flag (Query String Append)

RewriteCond %{HTTP_HOST} ^example.com [NC]  
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC,QSA]

Second Option

RewriteRule ^(.*)$ https://www.example.com/$1?%{QUERY_STRING} [L,R=301,NC]