0

Using nginx 1.8.1 on CentOS Linux, I am attempting to do the following:

  • Redirect traffic from server_name server1.olddomain.com; to server2.newdomain.com
  • In doing so, remove sites from the path.

For example, a modified snippet from nginx.conf.

server {
   ...
   server_name server1.olddomain.com;
   ...
   location /sites/ITDept {
        rewrite ^(.*) https://server2.newdomain.com/ITDept;
}

While this does work as expected for the exact rule above, anything else does not work.

https://server1.olddomain.com/sites/ITDept/subfolder/subfolder does not redirect as desired as https://server2.newdomain.com/ITDept/subfolder/subfolder. It redirects to https://server2.newdomain.com/sites/ITDept/subfolder/subfolder.

I've spent hours trying to figure out the correct syntax without success. I hope that there is someone that can be of assistance. Thank you in advance.

2 Answers 2

1

I may have just figured it out. By modifying the rewrite rule to this:

rewrite ^/sites/(.*)$ https://server2.newdomain.com/$1 last;

Seems to do the trick. Unless anyone has a better alternative. I'm still testing many links to determine if this is going to work, but so far so good. Thanks to this article. How to quick and easy remove part of an URL in Nginx with HttpRewriteModule?

0

Not sure what you want to do with URIs which do not begin with /sites, but a simple rewrite should be all that is required:

server {
    server_name server1.olddomain.com;
    rewrite ^/sites(.*)$ https://server2.newdomain.com$1 permanent;
    return 404;
}

See this document for details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .