2016-07-27 121 views
0

我在我的網站上創建了一個語言腳本。該腳本識別URL中的語言並加載語言定義。htaccess規則 - 多語言網站

例如:

RewriteRule ^(.+)/about/?$ sobre2.php?lang=$1 <-- working fine 
RewriteRule ^(.+)/contact/?$ contato2.php?lang=$1 <-- working fine 
RewriteRule ^(.+)/products/?$ produtos2.php?lang=$1 <-- working fine 
RewriteRule ^(.+)/contact/sales/?$ contato-vendas2.php?lang=$1 <-- working fine 
RewriteRule ^(.+)/contact/general/?$ contato-form2.php?lang=$1 <-- 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=$1 

而且它不工作,這也影響到所有其他網頁(和資源),所以資源沒有加載,頁面看起來像一個「純HTML」。

我想知道我怎麼可以配置htaccess的,使我的「index2.php」是這樣的訪問:

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

謝謝。

回答

1

.+將匹配任何一個或多個字符,如果您將規則放在其他規則之前,則會使其他規則無效。

您可以使用這些規則:

RewriteEngine On 
RewriteBase /b2b/ 

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

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

非常感謝你,它的工作完美。是否有可能檢測到有人訪問沒有語言代碼的網站,然後重定向它們?例如:如果用戶訪問鏈接「https://domain.com/b2b」,他將被重定向到「https://domain.com/b2b/pt-br」。 – paulohr

+0

嗯,它工作完美,但只在「索引」。我能做些什麼來使它在任何*頁面中工作?例如:如果用戶訪問「domain.com/b2b/contact」,他將被重定向到「domain.com/b2b/pt-br/contact」。我很抱歉沒有更好地解釋它。 – paulohr

+0

它沒有工作。即使輸入「/ pt-br /」,我也會收到404錯誤信息 – paulohr