简体   繁体   中英

.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';
}

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM