Mozinor
Mozinor

Reputation: 85

.htaccess for language detection

I need to work with virtual url for handling the language with folder or subfolder like

www.example.com/en/xxx/
OR www.example.com/xxx/en
OR www.example.com/xxx/xxx/en

And all give the same ?lang=en

Its possible ?

Thanks you

I tested this for subdomaine

RewriteRule ^(.+)/$ /index.php?a=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)$ /index.php?a=$1 [QSA,L]

# language
RewriteCond %{HTTP_HOST} !^(www.)?example.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+).example.com$
RewriteRule ^(.*)$ $1?ln=%1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(.+).example.com/$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

With PHP its work

$url = explode('/', $_GET['a']);
if(in_array('fr', $url)) {
$language = 'fr';
}
elseif(in_array('it', $url)) {
$language = 'it';
}
else {
$language = 'en';
}

Upvotes: 1

Views: 305

Answers (1)

Panama Jack
Panama Jack

Reputation: 24448

Well for this specific example.

example.com/xxx/en i need index.php?lang=en&a=xxx

You can use a rule like this.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/en/?$ /index.php?lang=en&a=$1 [L,QSA]

Upvotes: 3

Related Questions