.htaccess RewriteRule to redirect http to https messes up % escaped parameters in URL

Viewed 795

I'm using something similar to the following in my .htaccess file to redirect all http requests to https:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

That works, except for a situation when a URL is passed with escaped parameters, as such:

http://www.example.com/download.php?go=app%20name

when redirected, it is mistakenly escaped again into:

https://www.example.com/download.php?go=app%2520name

How do I fix this?

1 Answers

Use NE or noescape flag in your rule:

RewriteEngine on

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

Make sure to clear your browser cache before testing the change.

Related