doulanli6146 2017-09-05 03:13
浏览 51

如何在GRAV中重定向带有php扩展名的页面

I created a new web site using Grav. The old site has pages that had to be accessed using urls ending .php The url /somefolder/somepage.php just gives me "File not found". It does NOT lead me to the built-in Grav error page.

I disabled the error plugin of Grav so it does not get in the way.

How do I rewrite /somefolder/somepage.php to /somefolder/somepage
Also, how can I redirect any 404 error to the home page?
The error is handled by:

/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);
            }
        }

How can I replace the line "throw new \RuntimeException('Page Not Found', 404);" with an instruction to redirect to the home page?

The above error is only caught for urls NOT ending in .php
urls ending in .php are not handled by Grav, so I guess it's the web server handing those errors. The web server is nginx/1.11.9.
I tried adding the lines below to my nginx.conf but this did not resolve the problem.

error_page 404 = @foobar;

    location @foobar {
      rewrite  .*  / permanent;
    }
  • 写回答

1条回答 默认 最新

  • dongyao5186 2017-10-14 09:31
    关注

    I would handle both of your problems on server-side.

    How do I rewrite /somefolder/somepage.php to /somefolder/somepage

    I would do something like :

    location ~ \.php$ {
      if (!-f $request_filename) {
          rewrite ^(.*)\.php$ $1.html permanent;
      }
    }
    

    Which mean : for every php file requested, remove the php and replace .php by .html.

    Also, how can I redirect any 404 error to the home page?

    This problem may occur because of 2 common things :

    • http header problem : you don't change the http header in your redirect here so you still serve a 404. Before checking if the location exists, the server searches for the http code status. Here, it remains 404.
    • you may use fastcgi_intercept_errors on;

    For that problem, I would use this piece of code :

    server {
      ...
      index index.html index.php
      error_page 404 = @hpredirect;
      ...
      location @hpredirect {
        return 301 /;
      }
    }
    

    Hope it helps !

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集