{"id":1252,"date":"2023-04-23T18:04:07","date_gmt":"2023-04-23T17:04:07","guid":{"rendered":"https:\/\/organicdigital.co\/blog\/?p=1252"},"modified":"2026-05-18T00:37:57","modified_gmt":"2026-05-17T23:37:57","slug":"how-to-redirect-a-url-to-remove-a-directory-or-folder-name","status":"publish","type":"post","link":"https:\/\/daveashworth.co\/blog\/how-to-redirect-a-url-to-remove-a-directory-or-folder-name\/","title":{"rendered":"How To: Redirect A URL To Remove A Directory or Folder Name"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">As an experience <a href=\"https:\/\/daveashworth.co\/services-and-consultancy\/website-migrations-consultant\/\">website migration consultant<\/a> I have to say that a common scenario many websites face is the need to <a href=\"https:\/\/daveashworth.co\/blog\/a-rough-guide-to-30x-redirects\/\">301 redirect<\/a> a high number of URLs in the same way, in this case, by removing the last directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This was an issue I faced recently when I switched an AMP plugin off on my site to see if it had any affect on my blog visibility (it made no difference).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When AMP was in place, all blog posts were appended with the \/amp\/ folder, so:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/daveashworth.co\/blog\/how-to-implement-faq-schema-markup\/\">https:\/\/daveashworth.co\/blog\/how-to-implement-faq-schema-markup\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">became:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/daveashworth.co\/blog\/how-to-implement-faq-schema-markup\/amp\/\">https:\/\/daveashworth.co\/blog\/how-to-implement-faq-schema-markup\/amp\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, after disabling the plugin, Google would still come and try to crawl the old URLs which in turn generated the following crawl issues as reported by Search Console:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"727\" height=\"802\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/not_found_404.png\" alt=\"Not Found 404\" class=\"wp-image-1254\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/not_found_404.png 727w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/not_found_404-272x300.png 272w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/not_found_404-136x150.png 136w\" sizes=\"(max-width: 727px) 100vw, 727px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, there are only around 20 URLs to clean up, so I could easily create individual redirects either to implement directly in the .htaccess file or to upload via the <a href=\"https:\/\/wordpress.org\/plugins\/redirection\/\" target=\"_blank\" rel=\"noopener\">redirection wordpress plugin<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That said, it\u2019s not the most efficient way, and what if you have 000s of URLs like this?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then you use a redirect or rewrite rules that will automatically remove the \/amp\/ directory and 301 redirect back to the parent folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And this is how you do it:<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/daveashworth.co\/blog\/how-to-redirect-a-url-to-remove-a-directory-or-folder-name\/#Remove_A_Directory_or_Folder_Name_using_HTACCESS\" >Remove A Directory or Folder Name using HTACCESS<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/daveashworth.co\/blog\/how-to-redirect-a-url-to-remove-a-directory-or-folder-name\/#Remove_A_Directory_or_Folder_Name_using_NGINX\" >Remove A Directory or Folder Name using NGINX<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/daveashworth.co\/blog\/how-to-redirect-a-url-to-remove-a-directory-or-folder-name\/#Remove_A_Directory_or_Folder_Name_with_IIS\" >Remove A Directory or Folder Name with IIS<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Remove_A_Directory_or_Folder_Name_using_HTACCESS\"><\/span><strong>Remove A Directory or Folder Name using HTACCESS<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\nRewriteRule ^(.*?)\/amp\/$ $1\/ &#91;L,R=301]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code will capture any URLs that end with &#8220;\/amp\/&#8221; and redirect them to the same URL without the &#8220;\/amp\/&#8221; directory, using a 301 redirect. The &#8220;R=301&#8221; flag indicates a permanent redirect. The &#8220;L&#8221; flag tells Apache to stop processing any further rewrite rules if this rule is matched.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Remove_A_Directory_or_Folder_Name_using_NGINX\"><\/span><strong>Remove A Directory or Folder Name using NGINX<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if ($request_uri ~ \"^(.*?)\/amp\/$\") {\n    return 301 $1\/;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code uses an if statement to check if the requested URI matches the pattern &#8220;^(.*?)\/amp\/$&#8221;, which captures any string that ends with &#8220;\/amp\/&#8221; and captures everything before that in a backreference. The &#8220;return 301 $1\/;&#8221; statement then redirects the request to the captured string followed by a slash, effectively removing the &#8220;\/amp\/&#8221; directory from the URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that the use of if statements in NGINX can be inefficient and should be used sparingly. In this case, the performance impact should be minimal as the redirect rule is not likely to be triggered frequently. However, if you have a large number of redirects or complex redirect rules, it may be more efficient to use the NGINX Rewrite module or another redirect method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Remove_A_Directory_or_Folder_Name_with_IIS\"><\/span><strong>Remove A Directory or Folder Name with IIS<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open Internet Information Services (IIS) Manager and select the website or web application where you want to apply the redirect.<\/li>\n\n\n\n<li>Double-click the &#8220;URL Rewrite&#8221; icon to open the URL Rewrite module.<\/li>\n\n\n\n<li>Click the &#8220;Add Rule(s)&#8230;&#8221; link in the right-hand pane to create a new rule.<\/li>\n\n\n\n<li>In the &#8220;Add Rule(s)&#8221; dialog box, select &#8220;Blank Rule&#8221; and click &#8220;OK&#8221;.<\/li>\n\n\n\n<li>In the &#8220;Edit Inbound Rule&#8221; dialog box, give the rule a name (e.g. &#8220;Remove \/amp\/ directory&#8221;).<\/li>\n\n\n\n<li>Under &#8220;Requested URL&#8221;, select &#8220;Matches the Pattern&#8221;.<\/li>\n\n\n\n<li>In the &#8220;Pattern&#8221; field, enter the regular expression <code>^(.*?)\/amp\/$<\/code>. This pattern will match any URL that ends with &#8220;\/amp\/&#8221; and capture everything before that in a backreference.<\/li>\n\n\n\n<li>Under &#8220;Conditions&#8221;, click &#8220;Add&#8230;&#8221; to add a condition.<\/li>\n\n\n\n<li>In the &#8220;Add Condition&#8221; dialog box, select &#8220;Server Variable&#8221; for &#8220;Condition input&#8221;.<\/li>\n\n\n\n<li>In the &#8220;Server Variable&#8221; field, enter &#8220;HTTP_HOST&#8221;.<\/li>\n\n\n\n<li>In the &#8220;Check if input string&#8221; field, select &#8220;Matches the Pattern&#8221;.<\/li>\n\n\n\n<li>In the &#8220;Pattern&#8221; field, enter the hostname of your website (e.g. &#8220;example.com&#8221;).<\/li>\n\n\n\n<li>Under &#8220;Action&#8221;, select &#8220;Redirect&#8221;.<\/li>\n\n\n\n<li>In the &#8220;Action Properties&#8221; section, enter the following:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&#8220;Redirect URL&#8221;: <code>{R:1}\/<\/code><\/li>\n\n\n\n<li>&#8220;Redirect Type&#8221;: &#8220;Permanent (301)&#8221;<\/li>\n\n\n\n<li>&#8220;Append query string&#8221;: &#8220;Unchecked&#8221;<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"15\">\n<li>Click &#8220;Apply&#8221; to save the rule.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This rule will redirect any URL that ends with &#8220;\/amp\/&#8221; to the same URL without the &#8220;\/amp\/&#8221; directory, using a 301 redirect. Note that this rule assumes that your website is hosted on a single domain or hostname. If your website is hosted on multiple domains or hostnames, you will need to modify the hostname pattern in step 12 to match all of your hostnames.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And that&#8217;s it.  Another common use is if you decide to disable feeds on your site because no one uses them anymore.  In which case, follow the same steps as above but change \/amp\/ to \/feed\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find out how to redirect a set of URLs with the same directory appended using either .HTACCESS, NGINX or IIS server configs<\/p>\n","protected":false},"author":1,"featured_media":1253,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-1252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-crawl-rate","category-migrations"],"_links":{"self":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/1252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/comments?post=1252"}],"version-history":[{"count":1,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/1252\/revisions"}],"predecessor-version":[{"id":2129,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/1252\/revisions\/2129"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/media\/1253"}],"wp:attachment":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/media?parent=1252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/categories?post=1252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/tags?post=1252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}