多语言网站的.htaccess规则多语言、规则、网站、htaccess

2023-09-02 00:27:49 作者:酒断人情思

我重新设计一个PHP多语种的网站的URL(EN | ES ​​|代| FR | RU)。该URL的网站是这样的:

I'm redesigning the URL of a PHP multilingual site (en|es|de|fr|ru). The URL's of the site are this way:

www.mysite.com/page
www.mysite.com/page/subpage1
www.mysite.com/page/subpage1/subpage2
www.mysite.com/page/subpage1/subpage2/subpage3

多达四个级别的子目录(产品,子等)。语言被作为一个GET参数:

Up to four levels of subdirectories (products, subproducts, etc.). The language is passed as a GET parameter:

www.mysite.com/page?lang=es

www.mysite.com/page/subpage1/subpage2?lang=de

目前的.htaccess是这样的:

The current .htaccess is something like this:

Options +FollowSymlinks +MultiViews -Indexes
RewriteEngine on

RewriteBase /

# Redirect all versions of homepage to www.mysite.com
RewriteCond %{REQUEST_URI} ^/index.html$
RewriteRule ^index.html$ http://www.mysite.com/ [R=301,L]

# Redirect non-www to www traffic
RewriteCond %{HTTP_HOST} !^(www.mysite.com)?$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]

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

#Remove php extension
RewriteCond %{REQUEST_URI} ^(.*).php$
RewriteRule ^(.*)$ %1 [R=301,QSA] 

#Here I have a lot of 301 redirections, I ommit them for this example

# First level redirections
RewriteRule ^([0-9a-zA-Z-_]+)$ view?page=$1 [L,QSA]

# Second level redirections
#RewriteRule ^(page1|page2|page3)/([0-9a-zA-Z-+_,.()]+)$ view?page=$1&subpage1=$2 [L,QSA]

# Third level redirections
RewriteRule ^([0-9a-zA-Z-_]+)/([0-9a-zA-Z-+_,.()]+)/([0-9a-zA-Z-_]+)$ view?page=$1&subpage1=$2&subpage2=$3 [L,QSA]

# Fourth level redirections
RewriteRule ^([0-9a-zA-Z-_]+)/([0-9a-zA-Z-]+)/([0-9a-zA-Z-]+)/([0-9a-zA-Z-]+)$ view?page=$1&subpage1=$2&subpage2=$3&subpage3=$4 [L,QSA]

现在我想整个网站重定向到了prepended语言子目录中的网址是否存在于URL(GET)没有语言的参数,像这样的(默认的英语):

Now I want to redirect the whole site to a URL with a prepended language subdirectory if there is no language parameter in the URL (GET), like this (default english language):

www.mysite.com/page -> www.mysite.com/en/page
www.mysite.com/page/subpage1 -> www.mysite.com/en/page/subpage1

如果GET参数传递的网址,我想重定向到正确的网址:

and if the GET parameter is passed in the URL, I want to redirect to the correct URL:

www.mysite.com/page?lang=es -> www.mysite.com/es/page
www.mysite.com/page/subpage1?lang=es -> www.mysite.com/es/page/subpage1

我检查了其他类似的问题,但所有这些都设在301重定向与最后一个参数([R = 301,L]),我怕最后是无效的我。

I've reviewed other similar questions but all of them are based in a 301 redirect with last parameter ([R=301,L]), and I'm afraid that the Last is not valid for me.

我有几个问题:

有没有一种方法,使的.htaccess更容易,不必对每个级别的规则? 如何重定向我的整个网站以www.mysite.com/en~~V如果没有郎GET参数传递的网址是什么? 如何重定向到正确的本地化网站根据郎GET参数(www.mysite.com/page?lang=es - >重定向到www.mysite.com/es/page~~V)

在此先感谢

推荐答案

您需要这些额外的规则:

You need these additional rules:

# lang supplied in query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})(?:&|$) [NC]
RewriteRule !^[a-z]{2}/ /%1/%{REQUEST_URI}? [L,NC,R=301]

# lang not supplied in query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^lang=.+(&|$) [NC]
RewriteRule !^en/ /en/%{REQUEST_URI} [L,NC,R=301]