i have to permament redirect some old urls in nginx. the old urls are old-style php urls including a parameter for loading content. they look like this:


i want to redirect them to other urls like:


any advice on how i can achieve this? my tries failed. thanks in advance!


If you have a direct mapping between old URL parameters and new URL path, you could use such a syntax :

location /index.php {
  try_files $uri /$arg_site =404;

With such a config, any request starting with/index.php will first be tried againt the exact URL, then if there's no matching file or location, nginx will try an internal redirect with /foo if the original URL is /index.php?site=foo. If that fails too, nginx will send a 404 HTTP Not Found response.

The variable $arg_site is set to the value of the URL parameter site.

| improve this answer | |
  • okay thanks. actually the old and the new urls aren't the same. so e.g. index.php?site=foo AND index.php=site=bar should redirect to /home. – trnc Nov 23 '12 at 20:08

i fixed this issue by using php and not the nginx rewrite functionality:

if (isset($_GET["site"]) {
  switch ($_GET["site"]) {
    case 'foo':
      Header( "HTTP/1.1 301 Moved Permanently" ); 
      Header( "Location: /foo" ); 

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.