用于Web翻译的Mod_rewrite .htaccess

现在我有.htaccess想法:

lang参数可以是enlt

如果lang参数的值= en比它添加en/之后的域如下:

www.mysite.com/register.php?lang=en – > www.mysite.com/en/register/

如果lang参数的值= lt比它是这样的:

www.mysite.com/register.php?lang=lt – > www.mysite.com/register/

如果使用其他参数,则是这样的:

www.mysite.com/etc.php?id=123&param=abc – > www.mysite.com/etc/123/abc/

当我直接键入它的地址栏它工作正常,但问题是当点击button来改变语言。 我有导航和button来改变这样的语言:

 if (!empty($_GET['lang'])) { setcookie('lang', $_GET['lang']); } <form action="" method="get"> <input id="lang-lt" type="submit" name="lang" value="lt"> <input id="lang-en" type="submit" name="lang" value="en"> </form> <li><a href="index.php">Home</a></li> <li><a href="register.php">Register</a> 

ltenbutton被点击时它工作不正确,url看起来像:

www.mysite.lt/register/?lang=enwww.mysite.lt/?lang=enwww.mysite.lt/?lang=lt

代替

www.mysite.lt/en/register/www.mysite.lt/en/www.mysite.lt/

.htaccess文件现在看起来像:

 Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?lang=(lt)\s [NC] RewriteRule ^ %1? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?lang=(en)\s [NC] RewriteRule ^ %2/%1? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?lang=(en)&id=([^\s&]+)\s [NC] RewriteRule ^ %2/%1/%3? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?lang=(en)&id=([^\s&]+)&param=([^\s&]+)\s [NC] RewriteRule ^ %2/%1/%3/%4? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+\.php)\?lang=lt&(\S*)\s [NC] RewriteRule ^ %1?%2 [L,NE,R=302] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?id=([^\s&]+)\s [NC] RewriteRule ^ %1/%2? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?id=([^\s&]+)&param=([^\s&]+)\s [NC] RewriteRule ^ %1/%2/%3? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php\s [NC] RewriteRule ^ %1%2/ [R=302,L,NE] # url is ONLY '/en' or '/lt' -> redirect to /en/ or /lt/ (adding slash) RewriteRule ^(en|lt)$ $1/ [R=301,L,NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{QUERY_STRING} !(^|&)lang=(en|lt)(&|$) [NC] RewriteRule ^(?!(?:en|lt)/)(.+)$ lt/$1 [DPI,NC] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{DOCUMENT_ROOT}/$2.php !-f RewriteRule ^(en|lt)/([\w-]+)/?$ - [L] # now all urls have en/ lt/ -> parse them RewriteRule ^(en|lt)/([\w-]+)/?$ $2.php?lang=$1 [L,NC,QSA] RewriteRule ^(en|lt)/([\w-]+)/([\w-]+)/?$ $2.php?lang=$1&id=$3 [L,NC,QSA] RewriteRule ^(en|lt)/([\w-]+)/([\w-]+)/([\w-]+)/?$ $2.php?lang=$1&id=$3&param=$4 [L,NC,QSA] 

你有没有想到问题在哪里?

将您的排名前2的重定向规则更改为:

完成.htaccess:

 Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} \s/+(?:(?:index|(.+))\.php)?\?lang=lt\s [NC] RewriteRule ^ %1? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+([^.?]+)/?\?lang=lt\s [NC] RewriteRule ^ %1? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(?:(?:index|(.+))\.php)?\?lang=(en)\s [NC] RewriteRule ^ %2/%1? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+([^.?]+)/?\?lang=(en)\s [NC] RewriteRule ^ %2/%1? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?lang=(en)&id=([^\s&]+)\s [NC] RewriteRule ^ %2/%1/%3? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?lang=(en)&id=([^\s&]+)&param=([^\s&]+)\s [NC] RewriteRule ^ %2/%1/%3/%4? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+\.php)\?lang=lt&(\S*)\s [NC] RewriteRule ^ %1?%2 [L,NE,R=302] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?id=([^\s&]+)\s [NC] RewriteRule ^ %1/%2? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?id=([^\s&]+)&param=([^\s&]+)\s [NC] RewriteRule ^ %1/%2/%3? [R=302,L,NE] RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php\s [NC] RewriteRule ^ %1%2/ [R=302,L,NE] # url is ONLY '/en' or '/lt' -> redirect to /en/ or /lt/ (adding slash) RewriteRule ^(en|lt)$ $1/ [R=301,L,NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{QUERY_STRING} !(^|&)lang=(en|lt)(&|$) [NC] RewriteRule ^(?!(?:en|lt)/)(.+)$ lt/$1 [DPI,NC] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{DOCUMENT_ROOT}/$2.php !-f RewriteRule ^(en|lt)/([\w-]+)/?$ - [L] # now all urls have en/ lt/ -> parse them RewriteRule ^(en|lt)/$ index.php?lang=$1 [L,NC,QSA] RewriteRule ^(en|lt)/([\w-]+)/?$ /$2.php?lang=$1 [L,NC,QSA] RewriteRule ^(en|lt)/([\w-]+)/([\w-]+)/?$ /$2.php?lang=$1&id=$3 [L,NC,QSA] RewriteRule ^(en|lt)/([\w-]+)/([\w-]+)/([\w-]+)/?$ /$2.php?lang=$1&id=$3&param=$4 [L,NC,QSA]