Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用try\u文件从URL中删除带有nginx的子目录_Php_Nginx_Uri - Fatal编程技术网

Php 如何使用try\u文件从URL中删除带有nginx的子目录

Php 如何使用try\u文件从URL中删除带有nginx的子目录,php,nginx,uri,Php,Nginx,Uri,我一整天都在试图找到解决具体问题的办法 我的结构如下所示: location / { try_files $uri $uri/ /index.php?$uri&$args; } 现在,子目录的所有php文件也应该在/index.php中解析 location /subdir/ { try_files $uri $uri/ /index.php?$uri&$args; } 当我单击/subdir/thread.php?t=123时,$uri包含/subdir/thread.ph

我一整天都在试图找到解决具体问题的办法

我的结构如下所示:

location / {
try_files $uri $uri/ /index.php?$uri&$args;
}
现在,子目录的所有php文件也应该在/index.php中解析

location /subdir/ {
try_files $uri $uri/ /index.php?$uri&$args;
}
当我单击
/subdir/thread.php?t=123
时,
$uri
包含
/subdir/thread.php?t=123
。但是我希望
$uri
中只有
/thread.php?t=123
(没有subdir)


这可能吗?我不想使用重写,因为我将此脚本用于重定向脚本。

您需要使用正则表达式捕获要传递到
/index.php
URI
部分。使用
location
rewrite
实现此目的

使用正则表达式
位置

location ~ ^/subdir(/.*)$ {
    try_files $uri $uri/ /index.php?$1&$args;
}
location ~ \.php$ { ... }
正则表达式
location
块是按顺序计算的,因此块在配置文件中的位置很重要

要将子目录中的
.php
文件发送到
/index.php
,需要将此
位置
置于
位置\.php$
块上方

有关详细信息,请参阅


将命名的
位置
重写一起使用

location ^~ /subdir/ {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ~/subdir(/.*)$ /index.php?$1 last;
}
^ ~
操作符确保子目录中的
.php
文件也发送到
/index.php


rewrite
自动附加原始查询字符串。有关详细信息,请参阅。

这适用于静态文件。但是在subdir中,我也有PHP文件。你能帮帮我吗?是的,我没说到点子上。我已经更新了这两个解决方案,以涵盖子目录中的
.php
文件。