index.php 中包含文件的 Nginx 重写错误

Nginx rewrite error with including files in index.php

我有一个 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=;
}

这可以正常工作,但现在我想要包含在我的索引文件中的其他文件,如 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= [QSA,L]

所以我的问题是我如何让它工作以及我做错了什么?

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

/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,要么在页面的 header(在 <head></head> 内)添加一个基础:

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