I am trying to redirect all non-subdomained requests to www while preserving the request URI.
I am using this in my .htaccess file for the redirect:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
The problem though is that when I have a request like this:
example.com/search/?name=läkare
It redirects to:
www.example.com/search/?name=l%25C3%25A4kare
Which is kind of incorrect, it is encoding it twice. I check it with this:
<?php
echo rawurlencode('läkare');//outputs l%C3%A4kare
echo "\n";
echo rawurldecode('l%25C3%25A4kare');//outputs l%C3%A4kare
echo "\n";
echo rawurldecode(rawurldecode('l%25C3%25A4kare'));//outputs läkare
Why is it encoding it twice and how do I keep it from doing that? I am ok with 1 encoding but 2 is too much.
You need the NE
(no escape) rewrite flag for your rule. That will prevent the already escaped query string from getting double escaped:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With