How to Redirect URLs Using NGINX in different way
What is a Redirect?
A redirect is a web server function that will redirect traffic from one URL to another. Redirects are an important feature when the need arises. There are several different types of redirects, but the more common forms are temporary and permanent. In this article, we will provide some examples of redirecting through the vhost file, forcing a secure HTTPS connection, redirection to www and non-www as well as the difference between temporary and permanent redirects.
Note: As this is an NGINX server, any .htaccess rules will not apply. If you're using the other popular web server, Apache, you’ll find this article useful.
Common Methods for Redirects
Temporary redirects (response code: 302 Found) are helpful if a URL is temporarily being served from a different location. For example, these are helpful when performing maintenance and can redirect users to a maintenance page.
However, permanent redirects (response code: 301 Moved Permanently) inform the browser there was an old URL that it should forget and not attempt to access anymore. These are helpful when content has moved from one place to another.
How to Redirect
When it comes to NGINX, that is handled within a .conf file, typically found in the document root directory of your site(s), /etc/nginx/sites-available/directory_name.conf. The document root directory is where your site's files live, and it can sometimes be in the /html directory if you have one site on the server. Or if your server has multiple sites, it can be at /domain.com. Either way that will be yours .conf file name. In the /etc/nginx/sites-available/directory you'll find the default file that you can copy or use to append your redirects. Or you can create a new file name html.conf or domain.com.conf.
Note: If you choose to create a new file be sure to update your symbolic links in the /etc/nginx/sites-enabled. With the command:ln -s /etc/nginx/sites-available/domain.com.conf /etc/nginx/sites-enabled/domain.com.conf
The first example covered is the redirection of a specific page or directory to a new one.
1. Temporary Page-to-Page Redirect
server {
# Temporary redirect to an individual page
rewrite ^/oldpage$ http://www.domain.com/newpage redirect;
}
2. Permanent Page-to-Page Redirect
server {
# Permanent redirect to an individual page
rewrite ^/oldpage$ http://www.domain.com/newpage permanent;
}
3. Permanent www to non-www Redirect
server {
# Permanent redirect to non-www
server_name www.domain.com;
rewrite ^/(.*)$ http://domain.com/$1 permanent;
}
4. Permanent Redirect to www
server {
# Permanent redirect to www
server_name domain.com;
rewrite ^/(.*)$ http://www.newdomain.com/$1 permanent;
}
Sometimes the need will arise to change the domain name for a website. In this case, a redirect from the old site's URL to the new site's URL will be very helpful in letting users know the domain was moved to a new URL.
The next example we’ll cover is redirecting an old URL to a new URL.
5. Permanent Redirect to New URL
server {
# Permanent redirect to new URL
server_name olddomain.com;
rewrite ^/(.*)$ http://newdomain.com/$1 permanent;
}
We've added the redirect using the rewrite directive we 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. To achieve the permanent redirect, we add permanent after the rewrite directive as you can see in the example code.
When it comes to HTTPS and being fully secure it is ideal for forcing everyone to use https:// instead of http://.
6. Redirect to HTTPS
server {
# Redirect to HTTPS
listen 80;
server_name domain.com www.domain.com;
return 301 https://example.com$request_uri;
}
After these rewrite rules are in place, testing the configuration prior to running a restart is recommended. NGINX syntax can be checked with the -t flag to ensure there is not a typo present in the file.
NGINX Syntax Check
nginx -t
If nothing is returned the syntax is correct, and NGINX has to be reloaded for the redirects to take effect.
Restarting NGINX
service nginx reload
CentOS 7
Unlike CentOS 6, CentOS 7 uses systemd.
systemctl restart nginx