Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 250
Amir Avatar asked Jan 19 '26 06:01

Amir


1 Answers

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]
like image 139
anubhava Avatar answered Jan 20 '26 22:01

anubhava