Web Hosting Knowledge Base Web Hosting Knowledge Base
  • Home
Get Started
Web Hosting Knowledge Base Web Hosting Knowledge Base
Get Started
Web Hosting Knowledge Base Web Hosting Knowledge Base
  • Home
loading
  1. Home
  2. Getting Started
  3. Shared Hosting Quick Start Guide
  4. Steps to Redirect URLs Using Nginx
Updated on June 19, 2024

Getting Started

  • Folder icon closed Folder open iconShared Hosting Quick Start Guide
    • How to Change Nameservers in GoDaddy?
    • CloudLinux monitoring and limits
    • How to Create/Edit/Delete User Package with DirectAdmin?
    • How to Change the Time-zone in Webmail?
    • How to Add Email Filters from Webmail?
    • How to Claim Your Free .online Domain at Hostvento?
    • Managed WordPress Quick Start Guide
      • How to Update WordPress Installation?
      • How to Install WordPress Locally on Windows?
    • ProcessWire CMS
    • How to Buy Hosting from hostvento
    • Discover the Steps to Change a Password in Linux
    • How to change Domain Nameservers (DNS) of my Domain?
    • IP Deny Manager
    • What is SSH2 and how to install it
    • How to install eAccelerator on linux cPanel server ?
    • Do you support Magento eCommerce website
    • Discover the PHP Error Messages That Web Designers Should Know About
    • Learn to Use the Group Block in WordPress
    • How to determine the WordPress Version?
    • Learn to monitor blacklisted sites using Dropmysite
    • What is cPanel Hosting: A Complete Guide
    • How To Change The WordPress Auto-Save Interval?
    • Steps to Perform When the phprc File Isn’t Updating
    • How To Install Elgg Through Softaculous?
    • Steps to Redirect URLs Using Nginx
    • Using SMTP authentication to send email through WordPress
    • How To Configure The Display Options Of WooCommerce?
    • When Will Softaculous Provide With The Latest Version Of A Script/Application?
    • Know How To Set Constant FS_Method
    • How to Find Out the Latest Visitors to Your Website in cPanel?
    • How to Copy or Move Files in cPanel File Manager?
    • What Is a Parked(Alias) Domain and How Do I Set up One
    • Discover the Steps to Change a Password in Linux
    • How To Manage User Roles In Plesk?
    • How To Suspend And Unsuspend Accounts In WHM?
    • How to Connect to MySQL Using Python?
    • How To Change The Logo Of Your AbanteCart Store?
    • How To Create Custom Email Filters In cPanel?
    • How Do I Debug Magento MySQL Problems?
    • How to Add and Delete Email Account in Webuzo?
    • How to Add and Delete Email Account in Webuzo?
    • How to Add and Delete Email Account in Webuzo?
    • Learn to access phpMyAdmin and phpPgAdmin in cPanel
    • How to Restart a Server using Plesk?
    • SMF Hosting FAQ
    • Enable HTML files to display PHP Code
    • How to change your Plesk Password?
    • How to Check the Commission Status from Hostvento Affiliate Dashboard?
  • Folder icon closed Folder open iconUnmanaged VPS Quick Start Guide
  • Folder icon closed Folder open iconReseller Quick Start Guide
  • Folder icon closed Folder open iconUnmanaged Dedicated Server Quick Start Guide
  • Folder icon closed Folder open iconOrdering a hosting package
  • Folder icon closed Folder open iconHow to help secure your website
    • How To Secure Your Website's Software
  • Folder icon closed Folder open iconHostvento Hosting's Getting Started Guide
    • How to use the Knowledge Base
    • Shared Hosting Quick Start Guide
    • Managed WordPress Quick Start Guide
    • Reseller Quick Start Guide
    • Managed Dedicated Server Quick Start Guide
    • Unmanaged Dedicated Server Quick Start Guide
    • Ordering a hosting package
      • How to order a shared web hosting package
      • How to order a reseller hosting package
      • How to order an unmanaged Dedicated Server
      • How to order a managed Dedicated Server
      • How to use Hostvento Hosting coupons and discounts
    • Registering a domain
      • How to choose an effective domain name
      • How to protect your domain name
      • How to register and use internationalized domain names (IDNs)
      • .us Domain Name & Nexus Requirements Guide
      • Requirements for registering Canadian .ca domain names
      • Reactivating an expired domain
      • Validating your domain with ICANN
    • Configuring domain settings
      • Introduction to domain names and DNS
      • How to update the nameservers at third-party registrars
      • How to transfer a domain to Hostvento Hosting
      • How to set the name servers for a domain to point to Hostvento Hosting
      • How to access your web site before DNS propagation is complete
      • How to configure custom name servers
      • How to configure reverse DNS
      • How to set the name servers for a domain at OpenSRS
      • How to set the name servers for a domain registered at GoDaddy
      • How to change your account's primary domain name
      • How to use shared URLs to access your site
      • How to add an SRV record to a domain
      • How to use Anycast DNS with a web hosting account
      • What are subdomains, addon domains, and parked domains?
      • What are wildcard subdomains?
      • How to find the IP address for your hosting account
      • How to configure a .dk hosted custom name server
      • How to check a domain's automatic renewal status
      • How to verify site ownership with Google
      • What is a DNS TXT record?
    • Accessing your account
      • Accessing your hosting control panel
      • How to use SSH (Secure Shell) on Windows and Mac to access your account
      • How to access your account using SSH keys
      • Using FTP (File Transfer Protocol)
      • How to use Secure Copy Protocol(SCP) to transfer files securely on Windows and Mac
      • How to set up SFTP access for multiple users
      • How to use SSHFS (Secure Shell Filesystem)
      • How to use the SolusVM control panel to administer an unmanaged VPS
      • How to disable SSH logins for the root account
      • How to enable two-factor authentication for SSH
      • Warning message when you try to connect to a server using SSH
      • How to keep SSH connections alive
      • 'Output truncated' message when you try to list files using FTP
      • FTP accounts do not work after FileZilla upgrade
      • 'Too many connections from this IP' error message
      • How to change the SSH server port number
      • How to use FTP with Linux hosting accounts
    • How to set up e-mail for your web site
      • Configuring and accessing your e-mail
      • How to troubleshoot e-mail access problems
      • How to add signatures to webmail
      • An introduction to e-mail protocols
      • Accessing e-mail accounts through webmail
      • How to access e-mail accounts with client applications
      • How to troubleshoot e-mail accounts
      • E-mail client application setup
      • Messages are not copied to the Sent folder when using IMAP
      • How to view e-mail message headers
      • Using external SMTP servers to send e-mail
      • How to troubleshoot Apple (Mac) Mail
      • How to securely monitor e-mail using MailChannels
      • Using a catch-all e-mail address
    • How to migrate your data to Hostvento Hosting
      • Migrating an account from another web host
      • How to migrate e-mail from another host to Hostvento Hosting
      • Differences between site migrations and domain transfers
      • What is included and not included in cPanel migrations?
    • Backing up your data
      • How to back up data on shared hosting and reseller accounts
      • How to use Amazon S3 to back up and restore data
      • How to use Hostvento Cloud Backup
      • How to create e-mail backups
      • How to use Google Drive to back up and restore data
    • How to publish your web site
      • How to create and publish a site with the Hostvento Website Builder
      • How to publish your web site with Sandvox
      • How to publish your website with WebSite X5
      • How to publish your web pages with Google Web Designer
      • How to publish your web site with HTML-Kit
      • How to publish your web site with iWeb
      • How to publish your web site with Web Architect
      • How to publish your web site with PageBreeze
      • How to publish your web site with Dreamweaver
      • How to publish your web site with Web Easy Professional
      • How to publish your web site with Seamonkey
      • How to publish your web site with RapidWeaver
      • How to publish your web site with 90 Second Website Builder
      • How to publish your web site with Microsoft FrontPage
      • How to publish and edit files with UltraEdit
      • How to publish your web site with NetObjects Fusion
      • How to publish your web site with Freeway
      • How to publish and edit files with Sublime Text
      • How to publish your web site with Muse
    • Working with Linux
      • Introduction to Linux commands
      • How to work with compressed files on Microsoft Windows
      • How to edit text files from the command line
      • How to edit .htaccess files
      • How to compress files using cPanel or the command line
    • Internet and networking information
      • How to clear a web browser's cache and cookies
      • How to clear the DNS cache on your computer
      • How to clear the SSL state in a browser
      • Introduction to network troubleshooting
      • How to troubleshoot network connectivity using ping and traceroute
      • How to test DNS with dig and nslookup
      • How to troubleshoot network applications with telnet
      • How to troubleshoot SSL connections with the openssl program
      • Web pages display incorrectly on mobile devices
      • How to reduce backscatter and spam
      • How to check your website for broken links
      • How to troubleshoot network applications with curl
      • How to troubleshoot network applications with PowerShell and tnc
      • How to troubleshoot nameserver errors
      • Off-shore IP addresses
      • Google Chrome error message: "ERR_QUIC_PROTOCOL_ERROR"
      • How to set up a Wi-Fi hotspot on your mobile device
      • How to clear the DNS cache on macOS
      • How to change your DNS provider to reduce DNS propagation delays
    • Hostvento Hosting Policy FAQ
    • Why should I choose Hostvento Hosting?
    • What makes Hostvento Hosting’s service fast?
    • Solid State Drive (SSD) advantages
    • How To Start A Blog
    • How To Launch An eCommerce Website
    • How to determine your account's server name
    • Common web hosting terms and definitions
    • How to start a blog
      • How to start and run a blog
      • How to get started with WordPress
      • How to install and run a Nibbleblog site
      • Beyond WordPress basics
    • Top-level domain (TLD) availability
    • How to start a website on your hosting plan
    • How to determine which data center hosts your account
  • Folder icon closed Folder open iconEasy Website Software Setup Guide
  • Folder icon closed Folder open iconApplication optimization and configuration
    • How to enable the Redis extension for PHP
  • Folder icon closed Folder open iconSoftaculous 1-click application installer
    • When Will Softaculous Provide With The Latest Version Of A Script/Application?
    • How to use the Softaculous WordPress Manager
  • Folder icon closed Folder open iconHow to access the Hostvento Hosting Customer Portal
  • Folder icon closed Folder open iconHow to configure your domain to use Ezoic
  • Folder icon closed Folder open iconHow to back up and restore databases in Plesk
  • Folder icon closed Folder open iconHow to activate and configure Cloudflare ServerShield using the Plesk control panel
  • Folder icon closed Folder open iconHow to manage local backups on Dedicated servers
  • Folder icon closed Folder open iconHow to determine if your hosting account uses CloudLinux
  • Folder icon closed Folder open iconTurboHub: centralized, simplified WordPress site management from Hostvento Hosting

Steps to Redirect URLs Using Nginx

Estimated reading: 5 minutes 148 views

What is a Redirect?

Redirect is the name of a web server function that switches the traffic source of a URL for another. In situations where it is required, it is a crucial attribute. Although there are many different kinds of redirects, the two most often utilized are temporary and permanent. This article will explain the differences between temporary and permanent redirects, force an HTTPS connection to reroute to www and non-www, and provide some examples of redirecting using the vhost file.

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

The /home/s#/nginx/redirects.conf file is where you can set redirects if your website is hosted on the Nginx web server. Every site will have a unique s#, or FTP/SSH user, to itself. For assistance with a basic page-to-page redirect, you can download the “Redirection” plugin. Alternatively, you can create more precise redirects using the redirects.conf file or by following the previously provided examples.

Please get in touch with our help and request that Nginx be reloaded after you have correctly configured the rules in the redirects.conf file. If you feel uneasy completing the aforementioned steps, please get in touch with our support team via phone, chat, or ticket. Our support staff is here to assist you with this around-the-clock.

Still stuck? How can we help?

How can we help?

Was this page helpful? Yes No

Share this Doc

Steps to Redirect URLs Using Nginx

Or copy link

Clipboard Icon
CONTENTS
Leaf Illustration

© 2023 All Rights Reserved by Hostvento