tworabbits
tworabbits

Reputation: 1213

.htaccess redirect + rewrite: Prefix uri with language code while keeping initial uri

I am trying to rewrite/redirect incoming requests so that they will always be prefixed by a language code but would like to keep the originally requested path:

example.org/de/     -> rewrite to example.org?lang=de
example.org         -> redirect to example.org/de/ which will then be rewritten
                       to example.org?lang=de
example.org/en/     -> rewrite to example.org?lang=en
example.org/foo/bar -> redirect to example.org/de/foo/bar wich will 
                       then be rewritten to example.org/foo/bar?lang=de

These were my thoughts on this:

RewriteBase /

# No 'lang' key in query string 
RewriteCond %{QUERY_STRING} !lang=(en|de) 

# AND request_uri does not start with 'en' or 'de'
RewriteCond %{REQUEST_URI} !^(en|de) 

# Redirect to lang prefixed uri while keeping the initial uri
RewriteRule ^(.*)$ de/%{REQUEST_URI} [L,R=301,QSA]

# Add language prefix as query parameter
RewriteRule ^(en|de) ?lang=$1 [L,QSA]

Furthermore I would like to keep any query strings of the incoming request and append it to the redirected/rewritten uri. Actually my thoughts sound logic to me but I'm getting an endless redirect. I would be too happy if you could point me in the right direction since I'm lost since hours and hours

Update

Finally with the help of anubhava I could manage to solve this whole issue and would like to share the final solution

# 1) Prefix ALL request uris with /<lang>/. Depending on the `Accept-Language` 
#    header 'de' or the default 'en' is used as prefix.
# 2) Rewrite real files by stripping off the /<lang>/ prefix but still add 
#    the ?lang=<lang> query parameter
# 3) Add a trailing slash and append ?lang=<lang> query parameter


RewriteEngine On

RewriteBase /

# No 'lang' key in query string and 'de' as accepted language header.
# Redirect to 'de' prefixed uri
RewriteCond %{QUERY_STRING} !lang=(en|de) [NC]
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule !^(en|de)(/.*)?$ de%{REQUEST_URI} [L,NC,R]

# Redirect anybody without 'de' as accepted language to the 'en' version
RewriteCond %{QUERY_STRING} !lang=(en|de) [NC]
RewriteRule !^(en|de)(/.*)?$ en%{REQUEST_URI} [L,NC,R]

# Strip lang prefix in case this a real file
RewriteCond %{REQUEST_URI} ^/(en|de)(/.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}%2 -f 
RewriteRule ^(en|de)(/.*)$ $2?lang=$1 [L,NC]

# Add trailing slash to those request which make it till here
RewriteRule ^(en|de)(/.*)?/?$ $2/?lang=$1 [L,NC,QSA]

Thanks a lot for your help!

Upvotes: 3

Views: 2708

Answers (1)

anubhava
anubhava

Reputation: 785128

You can have your rules like this:

RewriteEngine On

# No 'lang' key in query string 
RewriteCond %{QUERY_STRING} !lang=(en|de) [NC]
# Redirect to lang prefixed uri while keeping the initial uri
RewriteRule !^(en|de)/ /de/%{REQUEST_URI} [L,NC,R=301]

RewriteRule ^(en|de)(/.*)?$ $2?lang=$1 [L,NC,QSA]

Test this after clearing your browser cache.

Upvotes: 2

Related Questions