{"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-09-23T01:17:56","modified_gmt":"2026-09-23T00:17:56","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 URLs To Remove A Directory or Folder"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>TL;DR:<\/strong> If you need to remove the same directory or folder from lots of URLs, you don\u2019t need to create individual redirects for every page. A single redirect rule can match the unwanted directory and 301 redirect each old URL to its equivalent without that folder. This guide shows how to do it using Apache <code>.htaccess<\/code>, NGINX and IIS, using old <code>\/amp\/<\/code> URLs from my own site as the example.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_90 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\/#Why_I_needed_to_remove_a_directory_from_multiple_URLs\" >Why I needed to remove a directory from multiple URLs<\/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_from_a_URL_using_htaccess\" >Remove a directory from a URL using .htaccess<\/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_part_of_a_URL_using_NGINX\" >Remove part of a URL using NGINX<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/daveashworth.co\/blog\/how-to-redirect-a-url-to-remove-a-directory-or-folder-name\/#Remove_a_directory_from_a_URL_using_IIS\" >Remove a directory from a URL using IIS<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/daveashworth.co\/blog\/how-to-redirect-a-url-to-remove-a-directory-or-folder-name\/#Removing_a_directory_from_a_URL_at_scale\" >Removing a directory from a URL at scale<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_I_needed_to_remove_a_directory_from_multiple_URLs\"><\/span>Why I needed to remove a directory from multiple URLs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As an experienced <a href=\"https:\/\/daveashworth.co\/services-and-consultancy\/website-migrations-consultant\/\">website migration consultant<\/a>, a common problem I come across is needing to <a href=\"https:\/\/daveashworth.co\/blog\/a-rough-guide-to-30x-redirects\/\">301 redirect<\/a> a large number of URLs that all follow the same pattern.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One example is removing a directory or folder from the URL while keeping the rest of the path unchanged.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I ran into this when I disabled the AMP plugin on my own site. While AMP was enabled, my blog posts had an additional <code>\/amp\/<\/code> directory, 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\">also had an AMP URL:<\/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\">After disabling the plugin, Google was still crawling the old <code>\/amp\/<\/code> URLs, which resulted in 404 errors being reported in Google 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\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example there were only around 20 URLs, so I could have created individual redirects in <code>.htaccess<\/code> or using 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\">But that&#8217;s not particularly efficient \u2014 especially if you&#8217;re dealing with hundreds or thousands of URLs following the same pattern.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A better solution is to use a rewrite or redirect rule that matches every URL ending in <code>\/amp\/<\/code> and permanently redirects it to the equivalent URL without that directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/blog\/example-post\/amp\/<\/code> \u2192 <code>\/blog\/example-post\/<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below I&#8217;ll show how to do that using <strong>Apache\/.htaccess, NGINX and IIS<\/strong>.<\/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_from_a_URL_using_htaccess\"><\/span><strong>Remove a directory from a URL 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 rule matches URLs ending in <code>\/amp\/<\/code> and permanently redirects them to the same URL with the <code>\/amp\/<\/code> directory removed.<br>For example:<br><br><code>\/blog\/example-post\/amp\/<\/code> \u2192 <code>\/blog\/example-post\/<\/code><br><br><code>R=301<\/code> tells Apache to return a permanent 301 redirect, while <code>L<\/code> tells it to stop processing further rewrite rules once this rule has matched.<br><br>If you need to remove a different directory, simply replace <code>amp<\/code> in the rule with the directory name you want to remove. For example, the same approach could be used for <code>\/feed\/<\/code>.<\/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_part_of_a_URL_using_NGINX\"><\/span><strong>Remove part of a URL using NGINX<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>rewrite ^\/(.*)\/amp\/$ \/$1\/ permanent;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This NGINX rewrite rule matches URLs ending in <code>\/amp\/<\/code> and permanently redirects them to the equivalent URL without that directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/blog\/example-post\/amp\/<\/code> \u2192 <code>\/blog\/example-post\/<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>(.*)<\/code> portion captures the URL path before <code>\/amp\/<\/code>, while <code>permanent<\/code> tells NGINX to return a 301 redirect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As with the <code>.htaccess<\/code> example above, you can adapt the rule to remove a different directory by replacing <code>amp<\/code> with the folder name you want to remove.<\/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_from_a_URL_using_IIS\"><\/span><strong>Remove a directory from a URL using 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 where you want to apply the redirect.<br><\/li>\n\n\n\n<li>Open URL Rewrite and click Add Rule(s)&#8230;<br><\/li>\n\n\n\n<li>Select Blank Rule and give the rule a name, such as Remove \/amp\/ directory.<br><\/li>\n\n\n\n<li>Under Requested URL, select Matches the Pattern.<br><\/li>\n\n\n\n<li>Enter ^(.*?)\/amp\/$ as the pattern. This matches URLs ending in \/amp\/ and captures the preceding URL path.<br><\/li>\n\n\n\n<li>Under Action, select Redirect.<br><\/li>\n\n\n\n<li>Set the Redirect URL to {R:1}\/.<br><\/li>\n\n\n\n<li>Set Redirect Type to Permanent (301) and leave Append query string unchecked.<br><\/li>\n\n\n\n<li>Click Apply to save the rule.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This permanently redirects a URL ending in <code>\/amp\/<\/code> to the equivalent URL without that directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/blog\/example-post\/amp\/<\/code> \u2192 <code>\/blog\/example-post\/<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As with the Apache and NGINX examples, you can change <code>amp<\/code> in the pattern if you need to remove a different directory from your URLs.<\/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=\"Removing_a_directory_from_a_URL_at_scale\"><\/span>Removing a directory from a URL at scale<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">And that&#8217;s it. Rather than creating hundreds or thousands of individual redirects, one rule can handle every URL that follows the same directory pattern.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Although I&#8217;ve used <code>\/amp\/<\/code> throughout these examples, the same approach works whenever you need to remove a consistent directory from a group of URLs. For example, if you disable WordPress feeds, you could adapt the rules above to redirect URLs ending in <code>\/feed\/<\/code> back to their parent URLs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whichever method you use, test the redirect before applying it across a large number of URLs. You want to make sure the old URL returns a <strong>301 redirect directly to the correct new URL<\/strong>, rather than creating a redirect chain or accidentally matching URLs you didn&#8217;t intend to change.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re making larger changes to your site&#8217;s URL structure, this becomes particularly important during a migration. I provide <a href=\"https:\/\/daveashworth.co\/services-and-consultancy\/website-migrations-consultant\/\">website migration consultancy<\/a> covering redirect planning, implementation and post-migration checks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/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":0,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/1252\/revisions"}],"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}]}}