简体   繁体   中英

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)

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM