Amir
Amir

Reputation: 35

Friendly php URL for multiple languages using htaccess file (detect language from URL)

I have a multi language php script which use URLs to detecting which language files should be load from template folder for user.

for example:

English version: site.com/etc.php or site.com/etc.php?lang=EN

Arabic version: site.com/etc.php?lang=AR

but I want to have more pretty and also SEO friendlier URLs. So I need to detect language by a nicer way from URLs.

for example:

English version: site.com/EN/etc.php

Arabic version: site.com/AR/etc.php

is this possible by .htaccess file? (I search for solutions in stackoverflow, but it didn't work...)

Thank you Amir

Upvotes: 3

Views: 1875

Answers (1)

anubhava
anubhava

Reputation: 785156

You can have these rules in your document root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+([^.]+\.php)\?lang=([^\s&]+) [NC]
RewriteRule ^ /%2/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Z]{2})/(.+?)/?$ /$2?lang=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Z]{2})/?$ /?lang=$1 [L,QSA,NC]

Upvotes: 4

Related Questions