问题
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