如何使用GRAV中的php扩展名重定向页面
问题描述:
我使用Grav创建了一个新网站。 旧网站的页面必须使用结束.php 的网址才能访问url /somefolder/somepage.php只是给了我“未找到文件”。 它不会引导我进入内置的Grav错误页面。如何使用GRAV中的php扩展名重定向页面
我禁用了Grav的错误插件,所以它不会妨碍。
如何重写/somefolder/somepage.php到/ somefolder/SomePage的
另外,我怎么可以重定向任何404错误到主页?
该错误是由处理:
/var/www/grav-admin/system/src/Grav/Common/Processors/PagesProcessor.php
if (!$page->routable()) {
// If no page found, fire event
$event = $this->container->fireEvent('onPageNotFound');
if (isset($event->page)) {
unset ($this->container['page']);
$this->container['page'] = $event->page;
} else {
throw new \RuntimeException('Page Not Found', 404);
}
}
我怎样才能更换行“throw new \ RuntimeException('Page Not Found',404);”有指示重定向到主页?
上述错误是只抓到对于网址不在.php结尾的.PHP
结尾的网址不被GRAV处理,所以我想它的Web服务器交给这些错误。网络服务器是nginx/1.11.9。
我试着将下面的几行添加到我的nginx.conf中,但是这并没有解决问题。
error_page 404 = @foobar;
location @foobar {
rewrite .*/permanent;
}
答
我会在服务器端处理您的两个问题。
如何重写/somefolder/somepage.php到/ somefolder/SomePage的
我会做这样的事情:
location ~ \.php$ {
if (!-f $request_filename) {
rewrite ^(.*)\.php$ $1.html permanent;
}
}
这意味着:对于要求每一个PHP文件,删除在PHP中,并通过.html替换.php。
此外,如何将任何404错误重定向到主页?
可能会出现此问题的原因是2个共性的东西:
- HTTP标头的问题:你不要在您的重定向更改HTTP头,你在这儿仍服务于404之前,如果位置检查存在,服务器搜索http代码状态。在这里,它仍然是404
- 您可以使用
fastcgi_intercept_errors on;
对于这个问题,我会用这段代码:
server {
...
index index.html index.php
error_page 404 = @hpredirect;
...
location @hpredirect {
return 301 /;
}
}
希望它能帮助!
我没有检查这个答案的正确性,因为Grave在管理页面中有一个设置,它已经这样做了。 Grav论坛上有人向我指出了这一点。不过谢谢你的回答。 –