Mr N Dynamite Mr N Dynamite - 9 months ago 80
Apache Configuration Question

Redirect subdirectory to root URLs using .htaccess

Slight tweak on some other questions/answers I found here, which didn't work for me.

My website had a subdirectory of where all the website's pages were located. I have now moved all the files to The /subdirectory has essentially been deleted.

How can I do a .htaccess redirect that makes any /subdirectory URL redirect to a URL without the subdirectory.

Here's an example:
now needs to be

Instead of doing a redirect on all the pages on my website, I'm hoping there's an easier way to just say "if someone requests a URL with /subdirectory in it, take them to exactly the same URL, just without /subdirectory at the start"

Answer Source

You can use this rule in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteRule ^subdirectory/(.*)$ /$1 [L,NC,R=302,NE]