GeekTantra
GeekTantra

Reputation: 12081

Apache .htaccess rewrite rule to redirect urls from sub-directory to parent directory

I am looking for an Apache .htaccess rewrite rule to redirect urls from sub-directory to parent directory.

Following is the use case:

URL Redirects:

http://localhost/content/videos redirects (302) to http://localhost/videos
http://localhost/content/article/some-awesome-article redirects (302) to http://localhost/article/some-awesome-article
http://localhost/content/some-html-file.html redirects (302) to http://localhost/some-html-file.html
etc.

While the following should not:

http://localhost/content/some-image.jpg
http://localhost/content/admin (Specific)

Upvotes: 1

Views: 196

Answers (1)

anubhava
anubhava

Reputation: 784918

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

RewriteEngine On

RewriteRule ^content/((?!admin|some-image\.jpg).+)$ /$1 [L,NC,R=302]

EDIT: Using RedirectMatch:

RedirectMatch 302 ^/content/((?!(admin|(.*\.(gif|jpeg|jpg|png|css|js|woff|ttf|svg|eot|GIF|JPEG|JPG‌​|PNG|CSS|JS|WOFF|TTF|SVG|EOT)))).+)$ /$1 

Upvotes: 3

Related Questions