我有一个项目,我必须将所有未找到的文件重定向到 index.php。我在 apache 中做了这个,在我的项目文件夹中放置了一个 .htaccess 文件。文件的内容是——
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Run Code Online (Sandbox Code Playgroud)
现在我想在 nginx 中使用同样的方法。这是我的 nginx.conf 的样子
root /usr/local/apache2/htdocs;
index index.php index.html index.htm;
location /project/ {
root /usr/local/apache2/htdocs;
index index.php index.html index.htm;
try_files $uri $uri/ index.php;
}
Run Code Online (Sandbox Code Playgroud)
现在每当我提出这样的请求时,http://localhost/project/hello
这个请求应该去,http://localhost/project/index.php
但这说File not found
。
现在为什么我认为这会起作用是因为由于root
location 块内的指令覆盖root
了块外的指令(尽管值相同),重写模块将查找/usr/local/apache2/htdocs/project/index.php
. 我在这里缺少什么?
更新
这个配置有效
root /usr/local/apache2/htdocs;
location /project/ {
index index.php index.html index.htm; #effectively root here is /usr/local/apache2/htdocs
try_files $uri $uri/ /project/index.php;
} …
Run Code Online (Sandbox Code Playgroud)