rink.attendant.6 rink.attendant.6 - 8 months ago 41
Apache Configuration Question

htaccess redirect based on query parameter value

My application used to do language switching using a

lang.php
file. A URL to redirect to English for
/path/to/page.php?foo=bar
would look like this:

/path/to/lang.php?lang=en-CA&uri=%2Fpath%2Fto%2Fpage.php%3Ffoo%3Dbar


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

/path/to/page.php?foo=bar&lang=en-CA


I'd like to be able to add a .htaccess file to all the locations that I have a
lang.php
file in order to keep the existing URL working without the
lang.php
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]


The
uri
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