phpapache.htaccesshttp-redirectmod-rewrite

.htaccess - redirect from subdirectory to root directory and remember in environment requested path


I have some difficulties with the mod_rewrite rules for language folders. I want to rewrite any request on:
www.example.com/lang/*
to:
www.example.com/*

I added a rule:

RewriteRule ^lang/(.*)$ \/$1

As I use several language I'd like to save in environment which exact language in URL is typed at first opening of site.

Further I use the rules:

SetEnvIf Referer "www.example.com/lang1/" LANG=1
SetEnvIf Referer "www.example.com/lang2/" LANG=2

So I have the language to show on site

However I could not get this environment when typing address www.example.com/lang1/ in browser.


Solution

  • As this is an internal rewrite (the new path is used instead of the old one), there is no communication with the client and therefore no new Referer set.

    But using the referer is always unreliable. Consider modifying the RewriteRule so that it contains the original request URI in the query string.

    This would be

    RewriteRule ^lang/(.*)$ \/$1&uri=%{REQUEST_URI}
    

    if your requests contain a query string, and

    RewriteRule ^lang/(.*)$ \/$1?uri=%{REQUEST_URI}
    

    if not. The original URI would then be contained in the variable $_GET['uri'].