[英]Nginx rewrite a folder with exclude
i am working on nginx webserver. 我正在使用Nginx Web服务器。
I want to redirect all urls inside folder1 www.site.com/folder1/ but not the subfolder1 www.site.com/folder1/subfolder1 我想重定向folder1 www.site.com/folder1/内的所有URL,但不重定向subfolder1 www.site.com/folder1/subfolder1
I created these rules to nginx configuration but no luck. 我为nginx配置创建了这些规则,但是没有运气。
location = /folder/subfolder {
位置= /文件夹/子文件夹{
}
}
location /folder {
位置/文件夹{
rewrite ^/folder(.*) www.redirect.com permanent;
重写^ / folder(。*)www.redirect.com永久;
}
}
Am i missing something? 我想念什么吗?
Ok so here's a refined answer including some of the comments I've read plus one of mine, 好的,这是一个精致的答案,其中包括我已阅读的一些评论以及我的其中一项,
to be able to access the assets inside the subfolder I added the try_files
, and the 301
redirect in all other urls was added for the redirection. 为了能够访问子文件夹中的资产,我添加了
try_files
,并在所有其他URL中添加了301
重定向以进行重定向。
location /folder/subfolder {
try_files $uri $uri/ =404;
}
location /folder {
return 301 $scheme://example.com;
}
Your new set of rules should be as follows. 您的新规则集应如下所示。 I am assuming that valid file hits are okay (ie the user knew the file).
我假设有效的文件命中是可以的(即用户知道该文件)。 If you do not want this behaviour, replace try_files with the content of the @rw block:
如果您不希望出现这种情况,请用@rw块的内容替换try_files:
location /folder {
try_files $uri $uri/ @rw;
}
location @rw {
rewrite ^/folder/([^\/]*) http://www.redirect.com/ permanent;
}
These should work. 这些应该工作。
Remove the "=" because that's for "exact" match. 删除“ =”,因为那是“完全”匹配。 So it only matches the folder itself, and a request for "/folder/subfolder/a_file.html" won't match that block.
因此,它仅与文件夹本身匹配,并且对“ /folder/subfolder/a_file.html”的请求将与该块不匹配。 Also you need to add $scheme in your rewrite rule.
另外,您还需要在重写规则中添加$ scheme。 And if you just want to redirect to the home page ( http://www.redirect.com ), you can remove the "$1" part.
而且,如果您只想重定向到主页( http://www.redirect.com ),则可以删除“ $ 1”部分。
location /folder/subfolder {
}
location /folder {
rewrite ^/folder(.*)$ $scheme://www.redirect.com$1 permanent;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.