Apache .htaccess重写规则将URL从子目录重定向到父目录

时间:2015-05-22 14:14:51

标签: regex apache .htaccess mod-rewrite redirect

我正在寻找一个Apache .htaccess重写规则来将URL从子目录重定向到父目录。

以下是用例:

网址重定向:

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.

虽然以下情况不应该:

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

1 个答案:

答案 0 :(得分:2)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On

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

编辑:使用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