bcoyour
bcoyour

Reputation: 133

.htaccess redirect subdirectory uri but not the subdirectory itself

I feel like I'm fairly close to this, but it's not quite right.

I'm trying to redirect anything after /subdirectory/ (Example: /subdirectory/admin) to the web root. For example /subdirectory/admin would redirect to /admin

But, I would like to still be able to go to /subdirectory/ the problem is that /subdirectory/ is also redirecting to /

Here is what I have and it works except that it still also redirects /subdirectory/

RedirectMatch 301 ^/subdirectory/(.*) http://example.com/$1

Any help would be much appreciated. Thank you!

Upvotes: 1

Views: 139

Answers (1)

anubhava
anubhava

Reputation: 785196

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^subdirectory/(.+)$ /$1 [L,NC,R=301]

Because of the use of .+ after subdirectory/ it will match subdirectory/foo or subdirectory/bar but will not match subdirectory/.

Upvotes: 1

Related Questions