{"id":750,"date":"2016-02-04T00:50:43","date_gmt":"2016-02-04T08:50:43","guid":{"rendered":"https:\/\/cloudinsidr.com\/content\/?p=750"},"modified":"2016-03-22T14:16:09","modified_gmt":"2016-03-22T22:16:09","slug":"regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites","status":"publish","type":"post","link":"https:\/\/www.cloudinsidr.com\/content\/regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites\/","title":{"rendered":"RegEx Bliss: Set Up URL Redirects in NGINX Using Regular Expressions without Rewrites"},"content":{"rendered":"<p>There are two major no-nos when it comes to writing NGINX redirects:<\/p>\n<ul>\n<li>thou shalt not use <del>if statements<\/del> (that&#8217;s a major no-no);<\/li>\n<li>thought shalt not use <del>rewrites<\/del>\u00a0(they are a waste of resources, as in &#8220;slow&#8221; and &#8220;expensive&#8221;).<\/li>\n<\/ul>\n<p>Instead, use ol&#8217; good location blocks. But what if you have plenty of URIs\u00a0to redirect? How do you deploy RegEx\u00a0magic with NGINX? Easy.<\/p>\n<p><!--more--><\/p>\n<p>For example, in order to eliminate the year, month and day from an URL, use this <strong>(updated!)<\/strong> regular expression:<\/p>\n<pre>location ~ \/magazine\/(\\d\\d\\d\\d)\/((\\d\\d)\\\/)+(.*){ \r\nreturn 301 http:\/\/$server_name\/magazine\/$4; \r\ntry_files $uri $uri\/ \/index.php?$args; \r\n}<\/pre>\n<p>The first capture group (\\d\\d\\d\\d) represents a four-digit\u00a0year, the second and third are nested and capture the month and the day (if available), followed by a forward slash and zero or more occurrences\u00a0of any characters (.*). The return directive will output only the fourth capture group [$4] that is defined as any number of characters other than the line break\u00a0(.+). The important thing to note is the fact that NGINX does not insist that\u00a0you mask forward slashes (\/) for reasons of simplicity (except when used in a capture group between brackets), yet it still counts capture groups the way you would expect it to.<\/p>\n<p>In case of doubt, you may want to refer to an online regex editor such as\u00a0<a href=\"http:\/\/regexr.com\/\" target=\"_blank\">regexr.com<\/a>\u00a0\u00a0or\u00a0<a href=\"https:\/\/regex101.com\/\" target=\"_blank\">regex101.com<\/a>. For those who need to refresh their regex memory, here is a nice <a href=\"http:\/\/www.rexegg.com\/regex-quickstart.html\" target=\"_blank\">cheat sheet for regular expressions<\/a>.<\/p>\n<p>Restart NGINX to apply configuration changes. (So far so good!)<\/p>\n<p>But what do you do to ensure that you haven&#8217;t overlooked\u00a0some God-forsaken URLs that will deliver nasty 404 errors until the cows come home? Easy: check the errors in Google Webmaster Tools (and also, read your email: should\u00a0you mess up in a major way, that&#8217;s where Google will complain, just before penalizing your site). Even if crawling\u00a0failed\u00a0only intermittently, it is still advisable to mark Google&#8217;s complaints as resolved once they have been\u00a0resolved, obviously.<\/p>\n<figure id=\"attachment_766\" aria-describedby=\"caption-attachment-766\" style=\"width: 600px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools.png\" rel=\"attachment wp-att-762\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-766\" src=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools-1024x263.png\" alt=\"Resolving pesky 404 errors in Google Webmaster Tools\" width=\"600\" height=\"154\" srcset=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools-1024x263.png 1024w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools-600x154.png 600w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools-300x77.png 300w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools-768x197.png 768w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools.png 1416w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-766\" class=\"wp-caption-text\">Resolving pesky 404 errors in Google Webmaster Tools<\/figcaption><\/figure>\n<p>To verify if your 301 redirects are working, check out this post:\u00a0<a href=\"https:\/\/www.digitalmastersmag.com\/magazine\/seo-secrets-how-to-verify-that-google-respects-your-301-and-302-redirects-ranking-wise\/\" target=\"_blank\">SEO Secrets: How to Verify that Google Respects Your 301 (and 302) Redirects, Ranking-Wise<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two major no-nos when it comes to writing NGINX redirects: thou shalt not use if statements (that&#8217;s a major no-no); thought shalt not use rewrites\u00a0(they are a waste of resources, as in &#8220;slow&#8221; and &#8220;expensive&#8221;). Instead, use ol&#8217; good location blocks. But what if you have plenty of URIs\u00a0to redirect? How do you [&hellip;]<\/p>\n","protected":false},"author":101011,"featured_media":766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[1,6],"tags":[7,29,102],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>RegEx Bliss: Set Up URL Redirects in NGINX Using Regular Expressions without Rewrites - CloudInsidr<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.cloudinsidr.com\/content\/regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"RegEx Bliss: Set Up URL Redirects in NGINX Using Regular Expressions without Rewrites - CloudInsidr\" \/>\n<meta property=\"og:description\" content=\"There are two major no-nos when it comes to writing NGINX redirects: thou shalt not use if statements (that&#8217;s a major no-no); thought shalt not use rewrites\u00a0(they are a waste of resources, as in &#8220;slow&#8221; and &#8220;expensive&#8221;). Instead, use ol&#8217; good location blocks. But what if you have plenty of URIs\u00a0to redirect? How do you [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.cloudinsidr.com\/content\/regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudInsidr\" \/>\n<meta property=\"article:published_time\" content=\"2016-02-04T08:50:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-03-22T22:16:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1416\" \/>\n\t<meta property=\"og:image:height\" content=\"364\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#website\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/\",\"name\":\"CloudInsidr\",\"description\":\"Cyber security, infotech\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.cloudinsidr.com\/content\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/02\/404_in_Google_Webmaster_Tools.png\",\"width\":1416,\"height\":364,\"caption\":\"Resolving pesky 404 errors in Google Webmaster Tools\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites\/#webpage\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites\/\",\"name\":\"RegEx Bliss: Set Up URL Redirects in NGINX Using Regular Expressions without Rewrites - CloudInsidr\",\"isPartOf\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites\/#primaryimage\"},\"datePublished\":\"2016-02-04T08:50:43+00:00\",\"dateModified\":\"2016-03-22T22:16:09+00:00\",\"author\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#\/schema\/person\/21ce63bea726ea64da1beed97e63ba84\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.cloudinsidr.com\/content\/regex-bliss-set-up-nginx-redirects-using-regular-expressions-and-say-goodbye-to-old-fashioned-rewrites\/\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#\/schema\/person\/21ce63bea726ea64da1beed97e63ba84\",\"name\":\"Anna E Kobylinska\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7a3e9bd152f9d5cd41bf2b92df649857?s=96&d=mm&r=g\",\"caption\":\"Anna E Kobylinska\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/750"}],"collection":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/users\/101011"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/comments?post=750"}],"version-history":[{"count":24,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/750\/revisions"}],"predecessor-version":[{"id":1065,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/750\/revisions\/1065"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/media\/766"}],"wp:attachment":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/media?parent=750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/categories?post=750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/tags?post=750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}