在Nginx中,我正在寻找将我的blog.example.com子域redirect到主网站( example.com/blog )的子目录。 blog.example.com子域在该子域下有许多博客post。 许多链接都指向这个旧的URL,我想为所有的blog.example.com/*链接(前一个子域的所有子目录链接)设置一个全部redirect,现在转到example.com/* (每篇文章应该位于example.com域的根目录中,而不是在单个post的/blog子目录中)。
我目前有子域redirect到example.com使用:
server { server_name blog.example.com; location / { return 301 https://example.com/blog; }
任何人有任何build议为以前的子域的所有子目录设置301的提示? (仅供参考:所有页面在example.com上已经存在同名)。
如果我正确地理解了你,你在旧站点上有两种types的URI,一种是embedded式的(即一个子目录),另一种是没有(单一的后置的)URI。
像这样的东西可能会工作:
location / { return 301 https://example.com/blog$request_uri; } location ~ ./ { return 301 https://example.com$request_uri; }
第二个位置块使用一个正则expression式来匹配任何URI,在第二个或后面的位置使用/。
你可以使用这个redirect:
server { server_name blog.example.com; return 301 https://example.com$request_uri; }
这会将每个url(例如https://blog.example.com/blog-post重写为https://example.com/blog_post 。