重定向目录的内容,目录本身及其在Nginx上的一些子目录和文件除外

时间:2015-12-12 04:04:06

标签: .htaccess redirect nginx

@Starkeen在Redirecting the content of a directory, but not the directory itself as well as some of its subdirectories and files处为Apache配置解决了这个问题,但我们很快就会转向Nginx。

他非常优雅的Apache解决方案:

RedirectMatch ^/category/((?!index|images|menu)[^/]+)/?$ /$1

此处,/category/目录中的所有内容(/category/index.php除外,/category/images/的内容和/category/menu/的内容)都会重定向到根文件夹。

我们尝试了从htaccess到http://winginx.com/en/htaccess提供的Nginx的翻译,

location ~ ^/category/((?!index|images|menu)[^/]+)/?$ {
rewrite ^(.*)$ /$1 redirect;
}

但这没有用。

1 个答案:

答案 0 :(得分:1)

出于某种原因,当我们最终放弃寻找解决方案并最终寻求帮助时,我们很快就会找到一个。这是 - 它完美地运作:

rewrite ^/category/((?!index|images|menu)[^/]+)/(.*)$ /$1 permanent;