Ivano Ivano - 1 month ago 25
PHP Question

htaccess language conditional redirect

I need to make a redirect in my htaccess, depending on the presence or not of the language code in the url.

Url structure should start with the domain followed by the language code and then, the specific page (as any regular site)


As you can see, the /en/ is the language code, but there could be different language codes [/en/es/it/]

So rules should be as follows:

  • language code must be present and it must be 2 characters long

if this rule is not matched, browser should be redirected to the default lang "/en/"

In my mind it is something like this... only in my mind ;)

RewriteCond %{REQUEST_URI} !^/([az]){2}/(.*)$
RewriteRule ^(.*)$ /en/$1 [L]

Thanks in advance


Your regex doesn't seem right and you can do this in RewriteRule itself:

RewriteEngine On

RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L]