Redirect ?lang=en string to subfolder /en

纵然是瞬间 提交于 2019-12-11 13:26:23

问题


I'd like to do a lot of redirects like this pattern:

domain.com/xxx/?lang=en to domain.com/en/xxx/

For example:

domain.com/about/?lang=en to domain.com/en/about/

This redirects all links */?lang=en to just one target.

RewriteCond %{QUERY_STRING} ^lang=en
RewriteRule ^ en/about/? [R=301,L]

回答1:


You can use these rules in your root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+([^/]+)/\?lang=([^\s&]+) [NC]
RewriteRule ^ /%2/%1? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([^/]+)/?$ $2/?lan=$1 [L,QSA]

Change 302 to 301 once you make sure code is working fine.



来源:https://stackoverflow.com/questions/24943513/redirect-lang-en-string-to-subfolder-en

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