{"id":2000,"date":"2023-11-08T16:08:18","date_gmt":"2023-11-08T10:38:18","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=2000"},"modified":"2025-03-17T11:48:22","modified_gmt":"2025-03-17T06:18:22","slug":"create-redirects-nginx","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/create-redirects-nginx\/","title":{"rendered":"How to Create Redirects with Nginx"},"content":{"rendered":"\n<p>A redirect is a way to send both users and search engines to a different URL from the one they initially requested. This guide will walk you through the basics of redirects and how to implement them using Nginx, making your website management journey smoother and more efficient. The simplicity of Nginx configuration files makes it an accessible tool for setting up redirects.<\/p>\n\n\n\n<p><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-create-redirects-in-nginx\">How to Create Redirects in Nginx<\/h3>\n\n\n<p>Creating redirects in Nginx is done by modifying the server configuration files. The primary configuration file for Nginx is located at <code>\/etc\/nginx\/nginx.conf<\/code>. Individual site configurations reside in <code>\/etc\/nginx\/sites-available<\/code> and are enabled by linking to <code>\/etc\/nginx\/sites-enabled<\/code>. Familiarizing yourself with these files is key to successfully managing redirects.<\/p>\n\n\n<h5 class=\"wp-block-heading\" id=\"implementing-a-301-permanent-redirect\">Implementing a 301 Permanent Redirect<\/h5>\n\n\n<p>A 301 redirect is used when a page has been moved permanently. To implement a 301 redirect in Nginx, you would add the following line to your server configuration block:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">rewrite ^\/old-page$ http:\/\/www.yourdomain.com\/new-page permanent;<\/pre>\n\n\n<h5 class=\"wp-block-heading\" id=\"implementing-a-302-temporary-redirect\">Implementing a 302 Temporary Redirect<\/h5>\n\n\n<p>A <a href=\"https:\/\/www.searchenginejournal.com\/301-vs-302-redirects-seo\/299843\/\" target=\"_blank\" rel=\"noopener\">302 redirect<\/a> is similar but is used when the move is temporary. The Nginx configuration line would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">rewrite ^\/old-page$ http:\/\/www.yourdomain.com\/new-page redirect;<\/pre>\n\n\n<h5 class=\"wp-block-heading\" id=\"creating-wildcard-redirects\">Creating Wildcard Redirects<\/h5>\n\n\n<p>For multiple URL changes, wildcard redirects are handy. With Nginx, you can use regular expressions to match patterns:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">rewrite ^\/old-folder\/(.*)$ http:\/\/www.yourdomain.com\/new-folder\/$1 permanent;<\/pre>\n\n\n<h5 class=\"wp-block-heading\" id=\"redirecting-www-to-nonwww\">Redirecting WWW to Non-WWW<\/h5>\n\n\n<p>Canonicalizing your domain is crucial for SEO. Here&#8217;s how you redirect the <code>www<\/code> version of your site to the non-<code>www<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">server {\n    server_name www.yourdomain.com;\n    return 301 $scheme:\/\/yourdomain.com$request_uri;\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"testing-and-troubleshooting-your-redirects\">Testing and Troubleshooting Your Redirects<\/h3>\n\n\n<p>After setting up redirects, testing them is essential to ensure they work as intended.<\/p>\n\n\n<h5 class=\"wp-block-heading\" id=\"how-to-test-redirects\">How to Test Redirects<\/h5>\n\n\n<p>You can test redirects by visiting the old URLs and observing whether you&#8217;re taken to the new location. Tools like <code>curl<\/code> are also handy for testing redirects non-interactively.<\/p>\n\n\n<h5 class=\"wp-block-heading\" id=\"solving-common-redirect-issues\">Solving Common Redirect Issues<\/h5>\n\n\n<p>If a redirect isn&#8217;t working, check for typos in the URL patterns or incorrect file paths. Ensure that you reload Nginx after making changes (<code>sudo systemctl reload nginx<\/code>) so that your updates take effect.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-verse has-black-color has-ast-global-color-7-background-color has-text-color has-background\">Avoid creating long chains of redirects, as they can slow down page load times and impact SEO. Consolidate multiple-step redirects into a single step where possible. And for complex redirect rules, Nginx allows the use of regular expressions. This is advanced usage and requires careful testing.<\/pre>\n\n\n\n<p><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"conclusion-and-further-learning\">Conclusion and Further Learning<\/h3>\n\n\n<p>You\u2019re now equipped with the knowledge to implement redirects using Nginx. While there is much more to learn, this guide provides a solid foundation for managing web traffic effectively.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/nginx.org\/en\/docs\/\" target=\"_blank\" rel=\"noopener\">Official Nginx Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Status\" target=\"_blank\" rel=\"noopener\">Mozilla Developer Network<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/forum.nginx.org\/\" target=\"_blank\" rel=\"noopener\">Nginx Community Forums<\/a><\/li>\n<\/ul>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-3d18d5a1 wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Unleash the Power of KVM VPS<\/h3><p class=\"uagb-cta__desc\">Experience lightning-fast performance and complete control with our cutting-edge KVM Virtual Private Servers. Built for speed and reliability.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/kvm-vps\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\"> Get Your VPS Now<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A redirect is a way to send both users and search engines to a different URL from the one they initially requested. This guide [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":7956,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15,5],"tags":[],"class_list":["post-2000","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers-stuff","category-linux"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/11\/How-to-Create-Redirects-with-Nginx.jpg",1366,768,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/11\/How-to-Create-Redirects-with-Nginx-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/11\/How-to-Create-Redirects-with-Nginx-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/11\/How-to-Create-Redirects-with-Nginx-768x432.jpg",768,432,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/11\/How-to-Create-Redirects-with-Nginx-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/11\/How-to-Create-Redirects-with-Nginx.jpg",1366,768,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/11\/How-to-Create-Redirects-with-Nginx.jpg",1366,768,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"A redirect is a way to send both users and search engines to a different URL from the one they initially requested. This guide [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=2000"}],"version-history":[{"count":8,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2000\/revisions"}],"predecessor-version":[{"id":8137,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2000\/revisions\/8137"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/7956"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=2000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=2000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=2000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}