Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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:

http://www.foo.com/index.php?site=foo
http://www.foo.com/index.php?site=bar

i want to redirect them to other urls like:

http://www.foo.com/news
http://www.foo.com/gallery

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

share|improve this question

2 Answers 2

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.

share|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. –  Tronic 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" ); 
      break;

     ...
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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