force user's language preference in the URL - rewriterule?

前端 未结 1 856
既然无缘
既然无缘 2021-01-07 09:30

I want to specify the user\'s language preference in the URL. Is it possible to use .htaccess rewrite rule to add a segment to the url, if missing.

URL should normal

相关标签:
1条回答
  • 2021-01-07 10:13

    Use a RewriteCond to look for the 2 letter lang code at the beginning of the URI:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # If the URL doesn't match /xx/otherstuff
    RewriteCond %{REQUEST_URI} !^([a-z]{2}/)(.*)$
    # Rewrite to /en/
    RewriteRule ^(.+)$ http://%{HTTP_HOST}/en/$1 [L,R,QSA]
    

    If you are using more than 2 character lang codes, like up to 4, use [a-z]{2,4} instead.

    0 讨论(0)
提交回复
热议问题