Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess Redirect URLs with UTF 8 chars

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.

like image 987
shiznatix Avatar asked Jan 23 '13 12:01

shiznatix


1 Answers

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]
like image 87
Jon Lin Avatar answered Sep 18 '22 12:09

Jon Lin