I want to specify the user\'s language preference in the URL. Is it possible to use .htaccess rewrite rule to add a segment to the url, if missing.
URL should normal
Use a RewriteCond
to look for the 2 letter lang code at the beginning of the URI:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If the URL doesn't match /xx/otherstuff
RewriteCond %{REQUEST_URI} !^([a-z]{2}/)(.*)$
# Rewrite to /en/
RewriteRule ^(.+)$ http://%{HTTP_HOST}/en/$1 [L,R,QSA]
If you are using more than 2 character lang codes, like up to 4, use [a-z]{2,4}
instead.