{"id":7091,"date":"2020-01-25T08:21:45","date_gmt":"2020-01-25T08:21:45","guid":{"rendered":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?p=7091"},"modified":"2020-01-25T08:21:45","modified_gmt":"2020-01-25T08:21:45","slug":"steps-to-redirect-urls-using-nginx","status":"publish","type":"post","link":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/","title":{"rendered":"Steps to Redirect URLs Using Nginx"},"content":{"rendered":"<h3>What is a Redirect?<\/h3>\n<p>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.<\/p>\n<div class=\"kb-wisdom-wrapr kb-wisdom-info kb-wisdom-wrapr-blue\">\n<div class=\"kb-wisdom-col kb-wisdom-col-icon\">\n<div class=\"kb-wisdom-icon\">\n<div><\/div>\n<\/div><\/div>\n<div class=\"kb-wisdom-col kb-wisdom-col-content\">\n<p><strong>Note:<\/strong> No .htaccess rules will apply as this is an Nginx server.<\/p>\n<\/div>\n<\/div>\n<h3>Common Methods for Redirects<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3>How to Redirect?<\/h3>\n<p>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\u2019s 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.<\/p>\n<p>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.<\/p>\n<div class=\"kb-wisdom-wrapr kb-wisdom-info kb-wisdom-wrapr-blue\">\n<div class=\"kb-wisdom-col kb-wisdom-col-icon\">\n<div class=\"kb-wisdom-icon\">\n<div><\/div>\n<\/div><\/div>\n<div class=\"kb-wisdom-col kb-wisdom-col-content\">\n<p><strong>Note:<\/strong> 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:<\/p>\n<pre class=\"lang:default decode:true \">ln -s \/etc\/nginx\/sites-available\/domain.com.conf \/etc\/nginx\/sites-enabled\/domain.com.conf<\/pre>\n<\/p>\n<\/div>\n<\/div>\n<p>First we will cover the example of redirection of a specific page\/directory to the new page\/directory.<\/p>\n<p><strong>Temporary Page to Page Redirect<\/strong><\/p>\n<pre class=\"lang:default decode:true \">server {\r\n# Temporary redirect to an individual page\r\nrewrite ^\/oldpage$ http:\/\/www.domain.com\/newpage redirect;\r\n}<\/pre>\n<p><strong>Permanent Page to Page Redirect<\/strong><\/p>\n<pre class=\"lang:default decode:true \">server {\r\n# Permanent redirect to an individual page\r\nrewrite ^\/oldpage$ http:\/\/www.domain.com\/newpage permanent;\r\n}<\/pre>\n<p><strong>Permanent www to non-www Redirect<\/strong><\/p>\n<pre class=\"lang:default decode:true \">server {\r\n# Permanent redirect to non-www\r\nserver_name www.domain.com;\r\nrewrite ^\/(.*)$ http:\/\/domain.com\/$1 permanent;\r\n}<\/pre>\n<p><strong>Permanent Redirect to www<\/strong><\/p>\n<pre class=\"lang:default decode:true \">server {\r\n# Permanent redirect to www\r\nserver_name domain.com;\r\nrewrite ^\/(.*)$ http:\/\/www.newdomain.com\/$1 permanent;\r\n}<\/pre>\n<p>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.<\/p>\n<p>Now we will check the example of redirecting an old URL to a new URL.<\/p>\n<p><strong>Permanent Redirect to New URL<\/strong><\/p>\n<pre class=\"lang:default decode:true \">server {\r\n# Permanent redirect to new URL\r\nserver_name olddomain.com;\r\nrewrite ^\/(.*)$ http:\/\/newdomain.com\/$1 permanent;\r\n}<\/pre>\n<p>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.<\/p>\n<p>In terms of HTTPS and being fully secure, it is ideal for forcing everyone to use https:\/\/ instead of http:\/\/.<\/p>\n<p><strong>Redirect to HTTPS<\/strong><\/p>\n<pre class=\"lang:default decode:true \">server {\r\n# Redirect to HTTPS\r\nlisten 80;\r\nserver_name domain.com www.domain.com;\r\nreturn 301 https:\/\/example.com$request_uri;\r\n}<\/pre>\n<p>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.<\/p>\n<p><strong>Nginx Syntax Check<\/strong><\/p>\n<pre class=\"lang:default decode:true \">nginx -t<\/pre>\n<p>In case nothing is returned the syntax is correct and Nginx has to be reloaded for the redirects to take effect.<\/p>\n<p><strong>Restarting Nginx<\/strong><\/p>\n<pre class=\"lang:default decode:true \">service nginx reload<\/pre>\n<p>For <strong>CentOS 7<\/strong> which unlike <strong>CentOS 6<\/strong>, uses systemd:<\/p>\n<pre class=\"lang:default decode:true \">systemctl restart nginx<\/pre>\n<h3>Redirects on Managed WordPress\/WooCommerce<\/h3>\n<p>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 \u2018Redirection\u2019 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.<\/p>\n<p>After having set the rules properly within the redirects.conf file, please contact our support and ask to reload Nginx. In case you aren\u2019t 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.<\/p>\n<p><strong>Also Read:<\/strong><\/p>\n<p><a href=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-redirects-in-cpanel\/\" target=\"_blank\" rel=\"noopener\"><strong>Learn to Configure Redirects in cPanel<\/strong><\/a><br \/>\n<a href=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/redirect-http-to-https-wordpress\/\" target=\"_blank\" rel=\"noopener\"><strong>Learn to Redirect HTTP to HTTPS in WordPress with Simple Steps<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[848,1229,1228,1230],"class_list":["post-7091","post","type-post","status-publish","format-standard","placeholder-for-hentry","category-web-hosting-faq","tag-nginx","tag-niginx-for-redirecting-urls","tag-redirect-urls","tag-redirecting-urls-using-nginx"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Steps to Redirect URLs Using Nginx<\/title>\n<meta name=\"description\" content=\"The article reveals steps to redirect URLs using Nginx along with different types of redirects and the syntax for them.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Steps to Redirect URLs Using Nginx\" \/>\n<meta property=\"og:description\" content=\"The article reveals steps to redirect URLs using Nginx along with different types of redirects and the syntax for them.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Hosting FAQs by MilesWeb\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-25T08:21:45+00:00\" \/>\n<meta name=\"author\" content=\"Pallavi Godse\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Pallavi Godse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/\",\"name\":\"Steps to Redirect URLs Using Nginx\",\"isPartOf\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website\"},\"datePublished\":\"2020-01-25T08:21:45+00:00\",\"author\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945\"},\"description\":\"The article reveals steps to redirect URLs using Nginx along with different types of redirects and the syntax for them.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Steps to Redirect URLs Using Nginx\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/\",\"name\":\"Web Hosting FAQs by MilesWeb\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945\",\"name\":\"Pallavi Godse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eefc9695ea2b2c6e143c9c9919701aaa?s=96&d=blank&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eefc9695ea2b2c6e143c9c9919701aaa?s=96&d=blank&r=g\",\"caption\":\"Pallavi Godse\"},\"description\":\"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.\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/author\/pallavi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Steps to Redirect URLs Using Nginx","description":"The article reveals steps to redirect URLs using Nginx along with different types of redirects and the syntax for them.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/","og_locale":"en_GB","og_type":"article","og_title":"Steps to Redirect URLs Using Nginx","og_description":"The article reveals steps to redirect URLs using Nginx along with different types of redirects and the syntax for them.","og_url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/","og_site_name":"Web Hosting FAQs by MilesWeb","article_published_time":"2020-01-25T08:21:45+00:00","author":"Pallavi Godse","twitter_misc":{"Written by":"Pallavi Godse","Estimated reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/","name":"Steps to Redirect URLs Using Nginx","isPartOf":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website"},"datePublished":"2020-01-25T08:21:45+00:00","author":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945"},"description":"The article reveals steps to redirect URLs using Nginx along with different types of redirects and the syntax for them.","breadcrumb":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/steps-to-redirect-urls-using-nginx\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/"},{"@type":"ListItem","position":2,"name":"Steps to Redirect URLs Using Nginx"}]},{"@type":"WebSite","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/","name":"Web Hosting FAQs by MilesWeb","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945","name":"Pallavi Godse","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eefc9695ea2b2c6e143c9c9919701aaa?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eefc9695ea2b2c6e143c9c9919701aaa?s=96&d=blank&r=g","caption":"Pallavi Godse"},"description":"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.","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/author\/pallavi\/"}]}},"views":974,"_links":{"self":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/7091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/comments?post=7091"}],"version-history":[{"count":1,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/7091\/revisions"}],"predecessor-version":[{"id":7092,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/7091\/revisions\/7092"}],"wp:attachment":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/media?parent=7091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/categories?post=7091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/tags?post=7091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}