Nginx重定向所有尝试访问文件夹的请求

时间:2013-12-30 00:03:55

标签: nginx rewrite

如何重定向尝试访问文件夹的所有请求?

例如,我想重定向:

somedomain.com/folder/subfolder/index.html
somedomain.com/folder/subfolder2/something.html
somedomain.com/folder/subfolder3/somethingelse.html

somedomain2.com/index.html

我尝试了什么:

if ( $request_uri = "/folder/.*" ) {
  rewrite ^/(.*)$  http://domain2.com/embed.html    permanent;
}

1 个答案:

答案 0 :(得分:1)

你只是无缘无故地复杂化,不需要做if条件,你应该总是尝试avoid it as much as possible

location /folder { # this will match all subfolders and files
    return 301 http://domain2.com/embed.html;
}