language parameter rewrite with mod_rewrite

元气小坏坏 提交于 2020-01-15 11:37:42

问题


I need some help with mod_rewrite doing multiple things to the url procedurally:

  • If the url starts with en,es,pt-br, remove it and add ?lang=$1

  • If the url does not next have 'web/' in it, add it.

  • If the url is blank, go to 'web/en/'

  • None of them should actually rewrite the url

This means:

 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:


Try:

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]


来源:https://stackoverflow.com/questions/12875284/language-parameter-rewrite-with-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!