How to redirect pages with php extension in GRAV


#1

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 any url ending in .php to the homepage /

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

Thanks.


#2

So you wish to redirect oldpage.php to the new page address?


#3

Hi,

Actually I would like to redirect any non existing page to the homepage (/)


#4

That’s quite simple :slight_smile:

  1. Enter the Error plugin on yoursite:/admin/plugins/error
  2. Turn it on
  3. Set 404 Route pointing to your home page (/ should be enough)

    Done!

#5

I had the error page turned on at first.
But… it would not catch the .php URLs.
The browser would just say “file not found”

I’m not a PHP programmer but a Javascript one.
Could you give some guidance on how to set the 404 Route to /
I did try a few things but what I tried, didn’t work.


#6

Did you set up everything as I asked in above post?
Please, set the Error plugin as shown on the screenshot, and let me know. In my case it redirects all of the pages, those ending with .php as well. But you have to enable the plugin, and point it to the custom route, in this case / (enter only slash where it asks for 404 Route)
It still doesn’t work?


#7

Let me set it up, just a few minutes.


#8

I’m sorry but it does not work.
Error plugin is turned on.

php1


#9

WIthout .php the error page gets called

php2


#10

I changed

$page = $pages->dispatch($this->config->get(‘plugins.error.routes.404’, ‘/error’), true);

to

$page = $pages->dispatch($this->config->get(‘plugins.error.routes.404’, ‘/’), true);


#11

Where did you change it?


#12

I replaced the content of error.md with the content of the home page.
That solved one part of the problem.
Any non-existing page not ending in .php will show the home page.

But URLs ending in .php will still give “file not found”

Funny because any other extension will get you to the error page.
Extensions like .asp or .xml work as expected (redirect to error page)


#13

In plugins/error/error.php


#14

Well, it’s the plugin code, I wouldn’t touch it if I were you :wink:
Okay, are you using Admin panel at all? Or are you making all changes on the files?


#15

I did change the files but it would be better to use the Admin panel.
I just don’t know how to fix my problem using the admin panel.

I restored the changes I made to the plugin. I just replaced the content of error.md with the content of my homepage.
That solves one part of my problem.

Now only the .php error is left.


#16

Okay. Go to this address:
http://earthfather.eu/admin/plugins/error
Do you see screen similar to one pasted by me in a post above?


#17

Yes, I see the same screen. It says enabled.


#18

Okay. Beneath the ‘enabled’ part, you have a field to enter ‘404 Route’, yes?
In this field, enter only the / symbol, nothing more, then save it


#19

Sorry, had to create a new account. New users can only send 25 messages :frowning:

Okay, that worked.

So the URL http://earthfather.eu/nothing brings me to the home page, great.

But http://earthfather.eu/nothing.php still gives me an empty browser screen with “File not found”

Please try the .php link


#20

Could it be caused by the way Grav is setup?
I am using a Docker container so I did not have to install PHP and other stuff I don’t know anything about.

I’m using this Docker container:

https://hub.docker.com/r/evns/grav/