2012-10-13 61 views
1

我需要一些幫助mod_rewrite的做多件事情的URL程序上:語言參數改寫與mod_rewrite的

  • 如果url有連接開始,ES,PT-BR,將其刪除,並添加LANG = $ 1

  • 如果該URL沒有在其中有'web /',請添加它。

  • 如果url爲空,進入 '網絡/ EN /'

  • 他們沒有實際上應該重寫URL

這意味着:

http://www.domain.com/en >> http://www.domain.com/web/?lang=en 
http://www.domain.com/en/mobile >> http://www.domain.com/mobile/?lang=en 

回答

1

嘗試:

RewriteEngine On 

# This is to prevent the rules from looping, they only work as on-shot 
RewriteCond %{ENV:REDIRECT_STATUS} 200 
RewriteRule^- [L] 

# If the url is blank, go to 'web/en/' 
RewriteRule ^/?$ /web/?lang=en [L,QSA] 

# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,has /web 
RewriteRule ^/?(en|es|pt-br)/web(/?.*)$ /web$2/?lang=$1 [L,QSA,R] 

# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,has no /web 
RewriteRule ^/?(en|es|pt-br)/?$ /web/?lang=$1 [L,QSA,R] 

# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,everything else 
RewriteRule ^/?(en|es|pt-br)/(.+?)/?$ /$2/?lang=$1 [L,QSA,R] 
+0

約翰,這似乎t o幾乎工作。我注意到,當進入domain.com/es/foo時,它不會加載domain.com/web/foo?lang=es,因爲它應該是。所有其他規則似乎工作。 –

+0

@LouisW是的,你給的例子是**'http://www.domain.com/en/mobile >> http://www.domain.com/mobile/?lang=en'**,所以我使用'http:// domain.com/es/foo'的規則並將其重定向到'http://omain.com/foo?lang = es',不是'/ web/foo'。 –

+0

請參閱步驟2,「如果網址中沒有'web /',請添加它。」由於不同的標準,它需要構建url。 –