Crawl Rate - Migrations

How To: Redirect A URL To Remove A Directory or Folder Name

By on 23rd April 2023

Reading Time: 3 minutes

  • LinkedIn
  • Twitter
301 redirect

As an experience website migration consultant I have to say that a common scenario many websites face is the need to 301 redirect a high number of URLs in the same way, in this case, by removing the last directory.

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).

When AMP was in place, all blog posts were appended with the /amp/ folder, so:


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:

Not Found 404

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 redirection wordpress plugin.

That said, it’s not the most efficient way, and what if you have 000s of URLs like this?

Then you use a redirect or rewrite rules that will automatically remove the /amp/ directory and 301 redirect back to the parent folder.

And this is how you do it:

Remove A Directory or Folder Name using HTACCESS

RewriteEngine On
RewriteRule ^(.*?)/amp/$ $1/ [L,R=301]

This code will capture any URLs that end with “/amp/” and redirect them to the same URL without the “/amp/” directory, using a 301 redirect. The “R=301” flag indicates a permanent redirect. The “L” flag tells Apache to stop processing any further rewrite rules if this rule is matched.

Remove A Directory or Folder Name using NGINX

if ($request_uri ~ "^(.*?)/amp/$") {
    return 301 $1/;

This code uses an if statement to check if the requested URI matches the pattern “^(.*?)/amp/$”, which captures any string that ends with “/amp/” and captures everything before that in a backreference. The “return 301 $1/;” statement then redirects the request to the captured string followed by a slash, effectively removing the “/amp/” directory from the URL.

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.

Remove A Directory or Folder Name with IIS

  1. Open Internet Information Services (IIS) Manager and select the website or web application where you want to apply the redirect.
  2. Double-click the “URL Rewrite” icon to open the URL Rewrite module.
  3. Click the “Add Rule(s)…” link in the right-hand pane to create a new rule.
  4. In the “Add Rule(s)” dialog box, select “Blank Rule” and click “OK”.
  5. In the “Edit Inbound Rule” dialog box, give the rule a name (e.g. “Remove /amp/ directory”).
  6. Under “Requested URL”, select “Matches the Pattern”.
  7. In the “Pattern” field, enter the regular expression ^(.*?)/amp/$. This pattern will match any URL that ends with “/amp/” and capture everything before that in a backreference.
  8. Under “Conditions”, click “Add…” to add a condition.
  9. In the “Add Condition” dialog box, select “Server Variable” for “Condition input”.
  10. In the “Server Variable” field, enter “HTTP_HOST”.
  11. In the “Check if input string” field, select “Matches the Pattern”.
  12. In the “Pattern” field, enter the hostname of your website (e.g. “”).
  13. Under “Action”, select “Redirect”.
  14. In the “Action Properties” section, enter the following:
  • “Redirect URL”: {R:1}/
  • “Redirect Type”: “Permanent (301)”
  • “Append query string”: “Unchecked”
  1. Click “Apply” to save the rule.

This rule will redirect any URL that ends with “/amp/” to the same URL without the “/amp/” 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.

And that’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/

Get In Touch

Fill in the form below if you want to enhance your website's organic visibility