I am handle with htaccess in my subdomain.
My htaccess script is given below
RewriteCond %{HTTP_HOST} ^demo\.example\.com/carrental$
RewriteRule (.*) carrental/([^/.]+)/([^/.]+)/([^/.]+) [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ carrental/locateaddress.php?country=$1&city=$2&locate=$3 [QSA]
RewriteCond %{HTTP_HOST} ^demo\.example\.com/carrental$
RewriteRule (.*) carrental/([^/.]+)/([^/.]+) [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ carrental/city.php?country=$1&city=$2 [QSA]
RewriteCond %{HTTP_HOST} ^demo\.example\.com/carrental$
RewriteRule (.*) carrental/([^/.]+) [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ carrental/country.php?country=$1 [QSA]
I have 2 problems.
I Want to pass special characters in my url.
I am already try (.*)
but its getting many problems. I want to pass special characters into ([a-zA-Z0-9_-]+)
.
My Major Query is i have one url. When my links translate to dutch language. Url : http://demo.osiztechnologies.com/carrental/Albanië
The problem is because of Albanië
. It shows a 404 error. If I change this into English it works fine.
How can I rewrite URL's with special characters?
The %{HTTP_HOST}
variable is the HTTP request's "Host:" header. It is only a hostname, no path information is given in that field. Thus:
RewriteCond %{HTTP_HOST} ^demo\.example\.com/carrental$
will never match. Not sure why it's there, as the resulting rule that the condition gets applied to is wrong as well:
RewriteRule (.*) carrental/([^/.]+)/([^/.]+)/([^/.]+) [R=301,L]
Here, you are matching the entire URI (via the (.*)
) and then redirecting the browser to:
/carrental/([^/.]+)/([^/.]+)/([^/.]+)
Note those ([^/.]+)
. They don't get replaced with anything, that's literally where you are sending the browser.
As far as the special characters. Rob Quist is only half right. While they do get encoded by the browser into escape sequences like %C3%AB
, the rewrite engine decodes them back into the unicode characters before applying any rules.
So, say you want to include ë
, then your rule will look like:
RewriteRule ^([a-zA-Z0-9_-]+)/([ëa-zA-Z0-9_-]+)$ carrental/locateaddress.php?country=$1&city=$2 [QSA]
You can stick all the possible unicode characters you expect to be getting inbetween the square brackets, but you can just make everything match easier by using the groupings similar to the ones in the broken rule:
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)$ carrental/locateaddress.php?country=$1&city=$2&locate=$3 [L,QSA]
RewriteRule ^([^/.]+)/([^/.]+)$ carrental/city.php?country=$1&city=$2 [L,QSA]
RewriteRule ^([^/.]+)$ carrental/country.php?country=$1 [L,QSA]
That won't work. You'd need to replace them with special characters (%C3%AB
in this case) in order to work.
The best solution here is to make 2 words for each record - a unique SEO-URL, and the real title.
Let the real title be "Albanië", and the SEO-version "albanie". Just do it for compatibilty's sake.
EDIT: Some browsers (such as Google Chrome) may translate %C3%AB
in a URL to ë
and back. Too bad the actual request sent to the server is the one with %C3%AB
.
EG: Your browser shows: site.com/locations/Albanië
Server receives: GET locations/Albani%C3%AB