Htaccess language Redirect 310 error

2019-09-11 12:27发布

I have a two language site

Swedish side  www.site.com
English side  www.site.com/?lang=en

I'm using

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (aa|ab|af|am|ar|as|ay|az|ba|be|bg|bh|bi|bn|bo|br|ca|co|cs|cy|da|de|dz|el|en|eo|es|et|eu|fa|fi|fj|fo|fr|fy|ga|gd|gl|gn|gu|ha|hi|hr|hu|hy|ia|ie|ik|in|is|it|iw|ja|ji|jw|ka|kk|kl|km|kn|ko|ks|ku|ky|la|ln|lo|lt|lv|mg|mi|mk|ml|mn|mo|mr|ms|mt|my|na|ne|nl|no|oc|om|or|pa|pl|ps|pt|qu|rm|rn|ro|ru|rw|sa|sd|sg|sh|si|sk|sl|sm|sn|so|sq|sr|ss|st|su|sw|ta|te|tg|th|ti|tk|tl|tn|to|tr|ts|tt|tw|uk|ur|uz|vi|vo|wo|xh|yo|zh|zu) [NC]
RewriteRule .* www.site.com/?lang=en [L]

To get all languages but Swedish to the www.site.com/?lang=en but I end up in a 310 loop. what is missing ?

1条回答
闹够了就滚
2楼-- · 2019-09-11 12:59

You need to add a check for lang to make sure you don't loop.

RewriteEngine on
RewriteCond %{QUERY_STRING} !(&|^)lang=
RewriteCond %{HTTP:Accept-Language} (aa|ab|af|am|ar|as|ay|az|ba|be|bg|bh|bi|bn|bo|br|ca|co|cs|cy|da|de|dz|el|en|eo|es|et|eu|fa|fi|fj|fo|fr|fy|ga|gd|gl|gn|gu|ha|hi|hr|hu|hy|ia|ie|ik|in|is|it|iw|ja|ji|jw|ka|kk|kl|km|kn|ko|ks|ku|ky|la|ln|lo|lt|lv|mg|mi|mk|ml|mn|mo|mr|ms|mt|my|na|ne|nl|no|oc|om|or|pa|pl|ps|pt|qu|rm|rn|ro|ru|rw|sa|sd|sg|sh|si|sk|sl|sm|sn|so|sq|sr|ss|st|su|sw|ta|te|tg|th|ti|tk|tl|tn|to|tr|ts|tt|tw|uk|ur|uz|vi|vo|wo|xh|yo|zh|zu) [NC]
RewriteRule .* http://www.site.com/?lang=%1 [L]

Note that the %1 is a backreference to the 2 letter language code that matched in the previous RewriteCond. The other thing about the Rule to note is that a request like /something/file.html will get redirected to http://www.site.com/?lang=(2 letter language code). Essentially, the original path is lost. If you want to keep it, you need to match and use a backreference:

RewriteRule ^(.*)$ http://www.site.com/$1?lang=%1 [L]

If you want previous query strings to also get passed, for example, /something/file.php?a=b, you need to add QSA in the brackets so that it will get redirected to http://www.site.com/somthing/file.php?lang=(2 letter language code)&a=b.

查看更多
登录 后发表回答