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've migrated my blog from wordpress to Octopress, and from one domain to another.

I want to create rewrites from old pages to new ones (old structure is http://domain.old/?p=number). I've created server block for this purpose which contains:

server {
    listen 80;
    listen [::]:80;
    server_name .domain.old;
    rewrite .*p=1$ https://domain.new/2011/rebirth/ permanent;
    rewrite .*p=11$ https://domain.new/2011/travelling-salesman-problem/  permanent;
    rewrite .*p=14$ https://domain.new/2011/minecraft-classic-client-with-fly-ability/ permanent;
#   rewrite ^ https://domain.new permanent;

If I uncomment last string I'm getting https://domain.new/p=14 for http://domain.old/p=14 request. If it's commented out, I'm getting some nginx welcome message, but no redirect.

I'm out of ideas. Any help appreciated.

My current nginx configuration.

share|improve this question
Hi Dmitry, I'm a little confused. If that line is uncommented, isn't it then doing what you want it to do, redirect from old to new? –  jmort253 Aug 11 '12 at 23:14
Last line redirecting any request to root directory of new blog. I need to redirect posts manually to right location, not just to the home page. –  Dmitry Verkhoturov Aug 11 '12 at 23:27

1 Answer 1

up vote 4 down vote accepted

A rewrite directive can not be applied to a $query_string and can only change $uri. To achieve what you are trying to do, you can do the following...

server {

  listen 80;
  listen [::]:80;
  server_name .domain.old;

  if ( $arg_p = 1 ) {
    return 301 https://domain.new/2011/rebirth/;

  if ( $arg_p = 11 ) {
    return 301 https://domain.new/2011/travelling-salesman-problem/;

  if ( $arg_p = 14 ) {
    return 301 https://domain.new/2011/minecraft-classic-client-with-fly-ability/;

  # other manual redirects
  # ...

  # global redirect
  return 301 https://domain.new;


Please note that IfIsEvil.

share|improve this answer
if is only evil if it contains directives other than those from the rewrite module (rewrite, return, set, etc.). –  mgorven Aug 12 '12 at 4:59
I was thinking about this kind of solution, but never seen any examples of use. Thank you very much! –  Dmitry Verkhoturov Aug 12 '12 at 7:10
@mgorven: Agreed. –  Pothi Aug 12 '12 at 7:47
@DmitryPaskal Glad to help. –  Pothi Aug 12 '12 at 7:48

Your Answer


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.