Nginx Redirects Examples

5 min read

Intro

Sometimes you might need to do different kinds of redirects that aren’t quite covered by our normal redirects. In this article, we’ll look at several redirect examples, and add to them over time.

Getting Started

To set up Nginx redirects you will first need to SSH into your server. To get started, please see the following articles:

1. Regular Nginx Redirects

Regular redirects are pretty straightforward with GridPane. We also have this dedicated KB article if you happen to be using the Redirection plugin:

Redirection Plugin: How to export redirects and set them via Nginx

Step 1. Create redirect-main-context.conf

We can add redirects via the *-main-context.conf include.

First, we need to create a file called redirect-main-context.conf inside /var/www/site.url/nginx . Create the file with the following command (switching out site.url for your domain name):

nano /var/www/site.url/nginx/redirect-main-context.conf

Next add your rewrites. The format order is as follows:

  1. rewrite
  2. URI to be redirected: ^/example-uri$
  3. Destination: /destination-uri or if it’s a different domain https://domain.com
  4. permanent

Putting it all together:

rewrite ^/example-uri$ /destination-uri permanent; 

Add you rewrites to your config. Multiple redirects can be added like so, one per line:

rewrite ^/example$ /sample-page permanent;
rewrite ^/another-example$ https://domain.com permanent;
rewrite ^/cart$ /basket permanent;

Now Ctrl+O and then press enter to save the file. Then Ctrl+X to exit nano.

Step 2: Check and reload Nginx

Finally, we need to check if the conf files are correct then reload Nginx.

Test your nginx syntax with:

nginx -t

If there are no errors present, reload nginx with the following command:

gp ngx reload

2. Old Domain to New Domain

This can be particularly useful f you’re redirecting full requests for existing site old-domain.url to new site new-domain.url, if on a different servers.

Let’s assume you want to redirect all traffic from an existing site with an old domain to a new site, with the full request URI.

If both sites are on the same server, it’s as simple as adding the old domain to the site as a redirect 301 domain via our domains manager, and swapping DNS.

But, if that old site still exists on another server and you want to keep that site up AND the DNS resolving to the old server, you can use one of our NginX includes.

In this instance it’s recommended to use the root include, which is located at /var/www/site.url/*-root-context.conf and is found in the standard Nginx Root location block in a GridPane virtual server like so:

location / { 
    ... stuff...
    try_files $uri $uri/ /index.php?$args;
    include /etc/nginx/extra.d/root-context.conf;
    include /var/www/old-domain.url/*-root-context.conf;
}

Using this example, we can create a file at:

/var/www/old-domain.url/new-domain-redirect-root-context.conf

with the following content:

return 301 https://new-domain.url$request_uri;

Check and reload Nginx

Test your nginx syntax with:

nginx -t

If there are no errors present, reload nginx with the following command:

gp ngx reload

3. Redirecting olddomain.com to newdomain.com/something

This example and the one that follows below instead make use of an alias domain to setup a redirect that is different to standard 301 redirect.

E.g. Directing olddomain.com > newdomain.com/en/

Step 1. Add your alias domain

Add the domain you want to redirect as an alias to your primary domain that you want it to redirect to.

Step 2. Setting your redirect

Inside your vhost you have this include (site.url = your domain name):

include /var/www/site.url/nginx/*-main-aliasdomain.url-context.conf;

So, using our example above, inside your server you could create the following inside your primary sites /nginx directory:

nano /var/www/olddomain.com/nginx/redirect-newdomain.com-context.conf

And then add your redirect:

return 301 https://newdomain/en$request_uri;

CTRL+O and then Enter to save the file, and then CTRL+X to exit nano.

Step 3: Check and reload Nginx

Test your nginx syntax with:

nginx -t

If there are no errors present, reload nginx with the following command:

gp ngx reload

You’re all set!

Note

Please note this will require an updated config, any alias domains configs updated since June 1st 2020 will have this include, however any older config that hasn't been regenerated recently will not. We do not automatically replace in place configs. The domain action such as changing www/root routing, or site actions such as changing cache types will regenerate any pre-existing config files to the latest version.

4. Redirecting olddomain.com/something to newdomain.com/somethingelse

Scenario 2: Inside of GridPane, you have the site “newdomain.url“.

You want to have any requests for your olddomain.url/something to redirect to newdomain.url/something-else .

Step 1. Add “olddomain.url” as an alias

Add the old domain olddomain.url as an alias inside the domains tab of newdomain.url site.

Step 2. Setting your Redirect

On your server create the following file:

/var/www/newdomain.url/nginx/redirects-main-olddomain.url-context.conf

Inside that file add the following:

location /something {
    rewrite ^(.*)$ https://www.newdomain.url/something-else redirect;
}

Step 3: Check and reload Nginx

Test your nginx syntax with:

nginx -t

If there are no errors present, reload nginx with the following command:

gp ngx reload

Important

As an additional note, editing any of the files located here:

/etc/nginx/sites-available/
Will get overritten at some point as there are many functions that will trigger the refreshing of these files. We add a "Do not edit directly" warning to the specific configs that are subject to change or that could cause potential stack issues when editing directly.