1

i'm not good in writing modrewrite access rules and would like to achieve the following scenario:

virtual url /en/shop.php redirects to /shop.php?lang=en

already got that but the actual problems are within the modrewrite environment:

if url is '/' (empty)     --> redirect to /en/  (/index.php?lang=en)
if url is '/en' or '/de'  --> redirect to /en/ or /de/ (add slash)
if an uri is "defined" like /en/shop.php -> redirect to /shop.php?lang=en

i've tried several rules but the last one results in a endless loop and i can't figure out what's wrong .. :/ please help

here's my .htaccess file:

RewriteEngine On
RewriteBase /   

# empty url -> redirect to en/
RewriteRule ^$ en/ [R=301,L]

# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$  $1/ [R=301,L]

# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$  $2?lang=$1&%{query_STRING} [R=301,L]
Fuxi
  • 329
  • 2
  • 6
  • 15

1 Answers1

2

This is the line that is causing the redirect loop:

RewriteRule ^(en|de)/(.*)$  $2?lang=$1&%{query_STRING} [R=301,L]

Because it is redirecting the browser to /?lang=en (for example). Then the first rule:

RewriteRule ^$ en/ [R=301,L]

Redirects it to /en/?lang=en, then the last rule redirects it to /?lang=en, then the first rule, etc.

You probably meant the last rule to be internal, additionally, your first rule needs to check if there's already a query string parameter, "lang":

RewriteEngine On
RewriteBase /   

# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ en/ [R=301,L]

# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$  $1/ [R=301,L]

# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$  $2?lang=$1&%{query_STRING} [L]
# no "R=301" here --------------------------------------^
Jon Lin
  • 142,182
  • 29
  • 220
  • 220