Search code examples
apache.htaccesshttp-redirecthttpsno-www

How to merge htaccess redirect by language function with www to non www and http to https? browsers return: err_too_many_redirects


i must get to work this 3 conditions because my website works on both english and spanish, depending the browser it must redirect to spanish or stay in english and i need to get sure that all my visitors reach it no matter they access via www or non www and has to always point to https in ES or EN scenario

RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ https://mywebsite/es/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(en|es)/?$ index.html?lang=$1 [QSA,NC,L]

Solution

  • Disclaimer: my speech doesn't answer your question directly.

    As previous comment mentioned, language redirects are suboptimal practice. The best way (and the one recommended by Google) is:

    • Step 1: at first visit guess user's language based on IP and browser language,
    • Step 2 (absolutely required step): show your guess to user (in a popup) and beg him to confirm your guess or to set his correct language,
    • Step3: After user action set a cookie with language selection, and on further visits redirect him according to htis cookie.

    Googlebot gets no cookies - it should crawl different language versions ONLY based on hreflang links.