rink.attendant.6 rink.attendant.6 -3 years ago 121
Apache Configuration Question

htaccess redirect based on query parameter value

My application used to do language switching using a

file. A URL to redirect to English for
would look like this:


Recently there has been some changes to accept a
query parameter on all pages. So this URL is nicer:


I'd like to be able to add a .htaccess file to all the locations that I have a
file in order to keep the existing URL working without the
file. Is this possible?

The RewriteRules must be completely relative to lang.php as the application is running on different hostnames and paths.

I took a stab at it based on the answer here but this is giving me a 404:

RewriteCond %{REQUEST_FILENAME} lang.php$
RewriteCond %{QUERY_STRING} (?:^|&)uri=([^&]+) [NC]
RewriteRule lang.php - [E=LANG_REDIR_URI:%1]

RewriteCond %{REQUEST_FILENAME} lang.php$
RewriteCond %{QUERY_STRING} (?:^|&)lang=([^&]+) [NC]
RewriteRule lang.php - [E=LANG_REDIR_LANG:%1]

RewriteCond %{REQUEST_FILENAME} lang.php$
RewriteRule . %{LANG_REDIR_URI}e [L,R=temporary]

parameter may already have query parameters (as in the examble above), and the code should not make the application vulnerable to an open redirect vulnerability.

Answer Source

You can use this rule in your site root .htaccess just below RewriteEngine On line:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(lang=[^&]+)&uri=([^&]+) [NC]
RewriteRule lang\.php$ /%2&%1? [L,NE,R=301]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download