apache - htaccess redirect based on query parameter value -


my application used language switching using lang.php file. url redirect english /path/to/page.php?foo=bar this:

/path/to/lang.php?lang=en-ca&uri=%2fpath%2fto%2fpage.php%3ffoo%3dbar 

recently there has been changes accept lang query parameter on pages. url nicer:

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

i'd able add .htaccess file locations have lang.php file in order keep existing url working without lang.php file. possible?

the rewriterules must relative lang.php application running on different hostnames , paths.

i took stab @ based on the answer here giving me 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 have query parameters (as in examble above), , code should not make application vulnerable open redirect vulnerability.

different cases:

  • uri query string, lang first: /foo/bar/lang.php?lang=en-ca&uri=%2ffoo%2fbar%2fpage.php%3fid%3d123 should redirect /foo/bar/page.php?id=123&lang=en-ca
  • uri without query string, lang first: /foo/bar/lang.php?lang=en-ca&uri=%2ffoo%2fbar%2fpage.php should redirect /foo/bar/page.php?lang=en-ca
  • uri query string, uri first: /foo/bar/lang.php?uri=%2ffoo%2fbar%2fpage.php%3fid%3d123&lang=en-ca should redirect /foo/bar/page.php?id=123&lang=en-ca
  • uri without query string, uri first: /foo/bar/lang.php?uri=%2ffoo%2fbar%2fpage.php&lang=en-ca should redirect /foo/bar/page.php?lang=en-ca

the order of query parameters not matter in redirect target.

update

after initial answer provided @ https://stackoverflow.com/a/45732670/404623, i've tried following .htaccess rules:

# works uri values without query strings  rewritecond %{query_string} ^(lang=[^&]+)&uri=([^&]+) [nc] rewriterule lang\.php$ /%2\?%1 [l,ne,r=temporary] rewritecond %{query_string} ^uri=([^&]+)&(lang=[^&]+) [nc] rewriterule lang\.php$ /%1\?%2 [l,ne,r=temporary]  # works uri values query strings  rewritecond %{query_string} ^(lang=[^&]+)&uri=([^&]+) [nc] rewriterule lang\.php$ /%2&%1? [l,ne,r=temporary] rewritecond %{query_string} ^uri=([^&]+)&(lang=[^&]+) [nc] rewriterule lang\.php$ /%1&%2? [l,ne,r=temporary] 

you can use rule in site root .htaccess below rewriteengine on line:

rewriteengine on  # uri query string     rewritecond %{query_string}     (?:^|&)(lang=[^&]+) [nc] rewritecond %1##%{query_string} (.*)##(?:.*&)?uri=([^?]+)(?:\?|%3f)([^&]+) [nc] rewriterule lang\.php$ /%2?%1&%3 [l,ne,r=302]  # uri without query string     rewritecond %{query_string}     (?:^|&)(lang=[^&]+) [nc] rewritecond %1##%{query_string} (.*)##(?:.*&)?uri=([^?]+) [nc] rewriterule lang\.php$ /%2?%1 [l,ne,r=302] 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -