Knowledge Base
MilesWeb / Web Hosting FAQ

Steps to Redirect URLs Using Nginx

Approx. read time : 4 min

What is a Redirect?

A web server function that redirects traffic from one URL to another is called as redirect. It is an important feature when there is a need of it. You will find several types of redirects, but those commonly used are temporary and permanent. In this article, we will offer some examples of redirecting via the vhost file, forcing a HTTPS connection redirection to www and non-www, and the difference between temporary and permanent redirects.

Note: No .htaccess rules will apply as this is an Nginx server.

Common Methods for Redirects

If a URL is temporarily being served from a different location, temporary redirects (response code: 302 Found) are helpful. For example, these are helpful for maintenance and can redirect users to a maintenance page.

Permanent redirects (response code: 301 Moved Permanently) notify the browser about the old URL that it should forget and not attempt to access further. These redirects prove helpful when content has moved from one place to another.

How to Redirect?

Nginx is handled within a .conf file located in the document root directory of your site(s), /etc/nginx/sites-available/directory_name.conf. Your site’s files live on the document root directory and it can be in the /html, if you have a single site on the server. In case, there are multiple sites on your server, the root directory can be at /domain.com.

May it be anything, it will be your .conf file name. You will find the default file in the /etc/nginx/sites-available/ directory that can be copied or used to append your redirects. Or a new file name html.conf or domain.com.conf can be created.

Note: In case you select to create a new file ensure to update your symbolic links in the /etc/nginx/sites-enabled with the below command:

ln -s /etc/nginx/sites-available/domain.com.conf /etc/nginx/sites-enabled/domain.com.conf

First we will cover the example of redirection of a specific page/directory to the new page/directory.

Temporary Page to Page Redirect

server {
# Temporary redirect to an individual page
rewrite ^/oldpage$ http://www.domain.com/newpage redirect;
}

Permanent Page to Page Redirect

server {
# Permanent redirect to an individual page
rewrite ^/oldpage$ http://www.domain.com/newpage permanent;
}

Permanent www to non-www Redirect

server {
# Permanent redirect to non-www
server_name www.domain.com;
rewrite ^/(.*)$ http://domain.com/$1 permanent;
}

Permanent Redirect to www

server {
# Permanent redirect to www
server_name domain.com;
rewrite ^/(.*)$ http://www.newdomain.com/$1 permanent;
}

Sometimes you will need to change the domain name for a website. Therefore, a redirect from the old sites URL to the new sites URL will be very helpful in helping users know the domain was moved to a new URL.

Now we will check the example of redirecting an old URL to a new URL.

Permanent Redirect to New URL

server {
# Permanent redirect to new URL
server_name olddomain.com;
rewrite ^/(.*)$ http://newdomain.com/$1 permanent;
}

The redirect is added using the rewrite directive discussed earlier. The ^/(.*)$ regular expression will use everything after the / in the URL. For example, http://olddomain.com/index.html will redirect to http://newdomain.com/index.html. For permanent redirect, we add permanent after the rewrite directive as seen in the example code.

In terms of HTTPS and being fully secure, it is ideal for forcing everyone to use https:// instead of http://.

Redirect to HTTPS

server {
# Redirect to HTTPS
listen 80;
server_name domain.com www.domain.com;
return 301 https://example.com$request_uri;
}

Now the rewrite rules are in place and it is recommended to test the configuration prior to running a restart. You can check Nginx syntax with the -t flag to ensure there is not a typo present in the file.

Nginx Syntax Check

nginx -t

In case nothing is returned the syntax is correct and Nginx has to be reloaded for the redirects to take effect.

Restarting Nginx

service nginx reload

For CentOS 7 which unlike CentOS 6, uses systemd:

systemctl restart nginx

Redirects on Managed WordPress/WooCommerce

If your website is running on Nginx web server, you can set the redirects through the /home/s#/nginx/redirects.conf file. Each site will have their own s# which is the FTP/SSH user per site. You can download the plugin called ‘Redirection’ to help with a simple page to page redirect, or else you can utilize the redirects.conf file for adding more specific redirects or you can even do that using the examples explained above.

After having set the rules properly within the redirects.conf file, please contact our support and ask to reload Nginx. In case you aren’t comfortable with performing the above steps, contact our support team via chat, ticket or a phone call. Our support team is available 24/7 to help you with this.

Also Read:

Learn to Configure Redirects in cPanel
Learn to Redirect HTTP to HTTPS in WordPress with Simple Steps

Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.

Trusted By Thousands of Clients & Big Businesses

We highly appreciate the kind and stellar feedback we receive from our customers. Delivering the best is our goal! MilesWeb is rated Excellent out of 5 based on reviews. Read more reviews.

Excellent
Based on reviews
2 hours ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
4 hours ago
Milesweb is superb Hosting pro...
Milesweb is superb Hosting provider ever, their Support team is amazing!!!...
Abhishek Singh
15 hours ago
Great support in great timing...
We need urgent assistance on changes in a primary domain on our client's Cpanel accounts and reached...
Riyaju Deen
21 hours ago
Best Website Hosting platform ...
I was new on MilesWeb. And needed help on multiple areas from setting up to getting started with cre...
Harshada
1 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
1 days ago
the team is very supportive th...
the team is very supportive though at times effort needs to be made to make understand the problem s...
Suree Sharma
1 days ago
I am using miles web for 3plus...
I am using miles web for 3plus years, very quick and perfect support by the team, they helped me man...
Sri Raghav
2 days ago
The service is good...
The service is good. They are answering with patience and doing the needful as soon as possible....
MR
2 days ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
3 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
4 days ago
positively helped me with find...
positively helped me with finding insecure content on my website causing SSL to not work properly on...
Thaviraj Junglee
4 days ago
Exceptional support, Truly Pra...
I had opted for the basic wordpress hosting plan as I intended to experiment with various plug-ins. ...
Aseem Chandna