我想重写一个像http://example.com/extras/?cat=help&page=faq
这样的网址到http://example.com/extras/help/faq
的行。Nginx重写规则导致错误403
那么,重写在链接或用户键入后者,服务器明白,它应该是前者。但是,在实现了表面上应该做我想做的重写之后,导航到页面会生成403代码,因为服务器正在使用网址栏字面上的。因为我将服务器设置为禁止直接访问子文件夹,所以返回403代码。
下面是我的网站上,处理加载页面被列入/extras
index.php文件中的PHP代码:
if(!empty($_GET['cat']) && !empty($_GET['page'])) {
$folder = $_GET['cat'];
$page = $_GET['page'] . '.php';
$pages = scandir($folder);
unset($pages[0], $pages[1]);
$url .= $folder . DIRECTORY_SEPARATOR;
if(file_exists($url . $page) && in_array($page, $pages)) {
$url .= $page;
include($url);
} else {
//Invalid category or page given
header("HTTP/1.0 404 Not Found");
}
} else {
//No category or page given; fall back to contents
include("contents.php");
}
上述文件的目的是为子文件的子文件夹中的内容包含在index.php的主体中,而不是让浏览器真正尝试并导航到子文件。
这是(部分)nginx的配置:
server {
listen 80 default_server;
listen [::]:80 ipv6only=on;
root /usr/share/nginx/html;
server_name localhost;
error_page 403 /;
error_page 404 /error/404.php;
error_page 500 502 503 504 /error/50X.php;
index index.html index.htm index.php;
location/{
try_files $uri $uri/ @no-extension;
allow 192.168.0.0/24;
allow 127.0.0.1;
deny all;
}
location /help {
try_files $uri $uri/ @help @no-extension;
}
location @help {
rewrite "^/help/([^/]*)/([^/]*)$" /help/?cat=$1&page=$2 last;
}
location ~ /help/(help|otherfolder|morefolders) {
deny all;
}
# PHP Handler
location ~ \.php$ {
try_files $uri $uri/ =404;
include fastcgi_params;
fastcgi_pass php5-fpm-sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
}
location @no-extension {
rewrite ^(.*)$ $1.php last;
}
}
什么办法可以使重写生效,而不是浏览器试图访问它永远无法到一个文件?
你认为指令'try_files $ uri $ uri/@help @ no-extension;'应该做什么? –
我更习惯于重写Apache,但我可以看到它可能是一个问题。但是,我无法删除$ uri $ uri/part,或者网站因为拒绝在“help”文件夹中加载index.php而中断。 – Flawedspirit
为了更直接地回答这个问题,我不知道。这只是其他位置块的“工作”,我预计它也会在这里。我急切地等待指导我出错的地方。 (我不是讽刺,我的意思是说。) – Flawedspirit