【问题标题】:Nginx redirect settingNginx 重定向设置
【发布时间】:2012-11-20 20:01:17
【问题描述】:

我的 Nginx 设置目前有这个:

 location / {
       if (!-e $request_filename){
            rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect;
       }
 }

基本上对于不存在的页面 (404),它会将用户重定向到主页。但是现在我在https://domain.com/blog/ 有一个wordpress 博客设置,但是任何wordpress 项目,例如。 https://domain.com/blog/test 也被重定向到主页。我想知道如何解决这个问题?

【问题讨论】:

    标签: wordpress nginx


    【解决方案1】:

    要为路径以“/blog/”开头的请求的 url 做一些不同的事情,请添加相应的位置,如下所示:

    location / {
       if (!-e $request_filename){ 
         rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect; }
    } 
    
    
    location /blog/ {
        #add in whatever directives are needed to serve your wordpress
    }
    

    【讨论】:

      【解决方案2】:

      您不应该使用if。请阅读 nginx wiki 上的 IfIsEvil 页面。相反,您应该使用try_files

      您的配置应该看起来更像这样:

      location / {
          try_files $uri $uri/ @notfound
      }
      
      location @notfound {
          rewrite ^(.*)$ https://domain.com/index.php?id=$1 redirect; 
      }
      

      真的,你根本不应该这样做。相反,您应该设置一个custom error 页面。将每 404 重定向到主页对您的 SEO 不利。

      编辑:我刚刚意识到您将 URL 传递给“id”。所以我删除了关于使用. 而不是^(.*)$ 的第二条评论。错误页面仍然是您最好的选择。您可以使用 $_SERVER['REQUEST_URI'] 获取 URL(如果您避免硬重定向)。

      这个page 有一些可能对您有帮助的示例配置。它有一些 wordpress 配置。

      【讨论】:

      • 正如您链接的 ifIsEvil 页面所解释的那样,带有返回或重写的 if 没有问题,所以这不是问题(尽管 try_files 通常是更好的方法)跨度>
      猜你喜欢
      • 2015-03-14
      • 2023-03-11
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2011-12-31
      相关资源
      最近更新 更多