htaccess 规则 - 多语言网站

htaccess rules - multi-language website

我在我的网站上创建了一个语言脚本。此脚本识别 URL 中的语言并加载语言定义。

例如:

RewriteRule ^(.+)/about/?$ sobre2.php?lang=    <-- working fine
RewriteRule ^(.+)/contact/?$ contato2.php?lang=  <-- working fine
RewriteRule ^(.+)/products/?$ produtos2.php?lang=  <-- working fine
RewriteRule ^(.+)/contact/sales/?$ contato-vendas2.php?lang=  <-- working fine
RewriteRule ^(.+)/contact/general/?$ contato-form2.php?lang=  <-- working fine

以上页面分别访问如下:

https://domain.com/b2b/pt-br/about/
https://domain.com/b2b/pt-br/contact/
https://domain.com/b2b/pt-br/products/
https://domain.com/b2b/pt-br/contact/sales/
https://domain.com/b2b/pt-br/contact/general/

这在除索引之外的所有页面中都工作正常。

当我尝试对索引页执行相同操作时,使用以下代码:

RewriteRule ^(.+)/?$ index2.php?lang=

除了不起作用之外,它还会影响所有其他页面(和资源),因此不会加载资源并且页面看起来像 "pure html"。

我想知道如何配置 htaccess 使我的 "index2.php" 可以像这样访问:

https://domain.com/b2b/pt-br/

谢谢。

.+ 将匹配 1 个或多个任意字符,如果您将规则放在其他规则之前,那么这将使其他规则无效。

您可以使用这些规则:

RewriteEngine On
RewriteBase /b2b/

RewriteRule ^((?!pt-br/).*)$ pt-br/ [L,R=301,NC]

RewriteRule ^(.+)/about/?$ sobre2.php?lang= [L,QSA]
RewriteRule ^(.+)/contact/?$ contato2.php?lang= [L,QSA]
RewriteRule ^(.+)/products/?$ produtos2.php?lang= [L,QSA]
RewriteRule ^(.+)/contact/sales/?$ contato-vendas2.php?lang= [L,QSA]
RewriteRule ^(.+)/contact/general/?$ contato-form2.php?lang= [L,QSA]
RewriteRule ^([a-z]{2}-[a-z]{2})/?$ index2.php?lang= [NC,L,QSA]