Dan Kanze
Dan Kanze

Reputation: 18595

www path to www2 redirect nginx

In my redirect rules I have a path that leads to a www2 site.

Redirecting to other www type sites work fine so I'm wondering if there is something special I need to add here that I'm missing. Is there cache I need to clear to propogate these changes?

location / {
    #This one doesn't work?
    if ( $request_filename ~ /foo0 ) {
        rewrite ^ http://www2.example.com/foo0 permanent;
    }

    #Works fine
    if ( $request_filename ~ /foo1) {
        rewrite ^ http://sub1.example.com/? permanent;
    }

    #Works fine
    if ( $request_filename ~ /foo2 ) {
        rewrite ^ http://sub2.example.com/? permanent;
    }

    #All other requests good.
    if (!-f $request_filename) {
        rewrite ^(.*)$ /index.php last;
    }
}

Upvotes: 1

Views: 113

Answers (1)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42899

You can replace these if's with something like this

location ~ /foo0$ {
  return 301 http://www2.example.com/foo0;
}
location ~ /foo1$ {
  return 301 http://sub1.example.com;
}
location ~ /foo2$ {
  return 301 http://sub2.example.com;
}
location / {
  try_files $uri /index.php$request_uri;
}

Avoid Taxing rewrites and using if

Upvotes: 1

Related Questions