【问题标题】:Nginx rewrite error with including files in index.phpNginx 在 index.php 中包含文件时重写错误
【发布时间】:2015-05-07 08:40:47
【问题描述】:

我有一个 apache 网络服务器,我在其中设置了一个 htaccess。我切换到 NGINX 并进行了 NGINX 配置。我的重写如下:

location /mvc/public {
     index index.php;

     try_files $uri $uri/ @rewrite;
 }

location @rewrite{
     rewrite ^/mvc/public/(.*)$ /mvc/public/index.php?url=$1;
}

这可以正常工作,但现在我想要包含在我的索引文件中的其他文件(如 css 和其他一些脚本)会出错。

我得到的错误是:

GET "example.com"/mvc/public/javascript/show/css/bootstrap.min.css 
GET "example.com"/mvc/public/javascript/show/css/simple-sidebar.css
GET "example.com"/mvc/public/javascript/show/css/custom.css 
GET "example.com"/mvc/public/javascript/show/js/bootstrap.min.js

当我转到“example.com”/mvc/public/javascript/show/examplecode. css的正确路径是“example.com”/mvc/public/css,js是“example.com”/mvc/public/js

我在 Apache 中使用的 HTACCESS 是:

Options -MultiViews
RewriteEngine On

RewriteBase /mvc/public

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

所以我的问题是如何让这个工作,我做错了什么?

【问题讨论】:

    标签: javascript css .htaccess nginx relative-path


    【解决方案1】:

    这看起来根本不像是重写问题。这看起来您的页面在其链接中具有相对 URL。当你去:

    /mvc/public/javascript/show/examplecode
    

    相对 URL 基数是 /mvc/public/javascript/show/ 所以当你有类似的 css 时:

    <link rel="stylesheet" href="css/custom.css" type="text/css">
    

    (请注意,href 中没有前导 /

    相对 URL 被附加到基础的末尾,使其成为:

    /mvc/public/javascript/show/css/custom.css 
    

    因此,您要么需要将链接全部更改为绝对 URL,要么在页面标题中添加一个基础(在 &lt;head&gt;&lt;/head&gt; 内):

    <base href="/mvc/public/" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2022-08-19
      • 2014-01-15
      相关资源
      最近更新 更多