skip to Main Content

We try to establish browser language (Accept-Language header) based redirects in our Shopware 6 systems as an addition to the standard .htaccess file.

We managed to do so for the root URLs, i.e.

example.com/ -> example.com/de/ (if the browser language is German)

works with the following .htaccess code:

# Redirect to language version based on browsers preferred language, default = de
# shop.example.de only
RewriteEngine On

RewriteCond %{HTTP_HOST} example [NC]
RewriteRule ^partner /de/account/login?campaignCode=partner&affiliateCode=partner [L,R=302]

RewriteCond %{HTTP_HOST} example [NC]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ /de%{REQUEST_URI} [L,R=302]

RewriteCond %{HTTP_HOST} example [NC]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en%{REQUEST_URI} [L,R=302]

RewriteCond %{HTTP_HOST} example [NC]
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ /es%{REQUEST_URI} [L,R=302]

RewriteCond %{HTTP_HOST} example [NC]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr%{REQUEST_URI} [L,R=302]

RewriteCond %{HTTP_HOST} example [NC]
RewriteRule ^$ /de%{REQUEST_URI} [L,R=302]

# BEGIN Shopware
# The directives (lines) between "# BEGIN Shopware" and "# END Shopware" are dynamically generated. Any changes to the directives between these markers will be overwritten.

DirectoryIndex index.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by Apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch ".(?i:svg)$">
        Header set Content-Security-Policy "script-src 'none'"
    </FilesMatch>
</IfModule>

# END Shopware

No we want to keep path’s intact

example.com/contact -> example.com/de/contact

Also we want to make the /partner redirect working for all languages.

We tried this but are getting redirect loops:

RewriteCond %{HTTP_HOST} example [NC]
RewriteRule ^partner /de/account/login?campaignCode=partner&affiliateCode=partner [L,R=302]

RewriteCond %{HTTP_HOST} example
RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^(.*)$ /de/$1

RewriteCond %{HTTP_HOST} example
RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^(.*)$ /en/$1

RewriteCond %{HTTP_HOST} example
RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^(.*)$ /es/$1

RewriteCond %{HTTP_HOST} example
RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^(.*)$ /fr/$1

RewriteCond %{HTTP_HOST} example
RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
RewriteRule ^(.*)$ /de/$1
AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.,

2

Answers


  1. Chosen as BEST ANSWER

    I think I found a solution:

    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteCond %{HTTP_HOST} example [NC]
    RewriteRule ^(de|en|es|fr)/partner /$1/account/login?campaignCode=partner&affiliateCode=partner [L,R=302]
    
    RewriteCond %{HTTP_HOST} example
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
    RewriteCond %{HTTP:Accept-Language} ^de [NC]
    RewriteRule ^(.*)$ /de/$1 [END,R=302]
    
    RewriteCond %{HTTP_HOST} example
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
    RewriteCond %{HTTP:Accept-Language} ^en [NC]
    RewriteRule ^(.*)$ /en/$1 [END,R=302]
    
    RewriteCond %{HTTP_HOST} example
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
    RewriteCond %{HTTP:Accept-Language} ^es [NC]
    RewriteRule ^(.*)$ /es/$1 [END,R=302]
    
    RewriteCond %{HTTP_HOST} example
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
    RewriteCond %{HTTP:Accept-Language} ^fr [NC]
    RewriteRule ^(.*)$ /fr/$1 [END,R=302]
    
    RewriteCond %{HTTP_HOST} example
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !^/(de|en|es|fr)/
    RewriteRule ^(.*)$ /de/$1 [END,R=302]
    
    
    
    # BEGIN Shopware
    # The directives (lines) between "# BEGIN Shopware" and "# END Shopware" are dynamically generated. Any changes to the directives between these markers will be overwritten.
    ...
    

    So I replicated parts of the Shopware standard rules and excluded index.php from matching as well


  2. Thx, seems to work. But I have to add:

    RewriteCond %{HTTP_HOST} example
    RewriteCond %{REQUEST_URI} !^/admin
    RewriteCond %{REQUEST_URI} !^/bundles
    RewriteCond %{REQUEST_URI} !^/api
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !^/(de|en|es|fr|it)/
    RewriteRule ^(.*)$ /de/$1 [L,R=301]
    

    for calling the adminstration.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search