Fixing Grav Admin Panel URL redirect on a local computer

Posted

While testing out Grav, it’s quite easy to setup a local instance on a macOS computer, since macOS already comes with a suitable web server (Apache 2.4.x and PHP 7.x). I never realized I didn’t actually describe how I set it up, but just wanted to point out one important change.

First off, follow this guide Enabling PHP in Apache on macOS Catalina 10.15, or for macOS Mojave10.14.

Grab the Grav skeleton of your choice and extract everything into a subfolder in your www root. My preference is to use a subfolder, since I maintain a few web sites and want to test, e.g. different versions of Grav. When extracting, make sure the hidden .htaccess file is in the folder!

In Grav 1.5.x (early 2019), I’d just edit the default .htaccess file in each subfolder, e.g. www/ver1/.htaccess or www/ver2/.htaccess to add RewriteBase. This is used to specify the web root, so that Grav need not be aware that it is in a subfolder. In the first case, for example I’d add it after the RewriteEngine On line:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /ver1
  ...
</IfModule>
Options -Indexes
DirectoryIndex index.php index.html index.htm

And, to login to the Grav Admin Panel UI, I’d head over to localhost/ver1/admin.

However, “recently”, around v1.6.x (middle 2019, though I’m not 100% certain), I noticed this stopped working. Logging in to my Admin Panel UI would redirect me back to the root i.e. localhost/ver1 and I would not be able to actually get anywhere! Specifically, the browser would receive this header:

HTTP/1.1 302 Found

To cut a long story short - this is the solution, which is kinda like Scenario 2 in the Grav documentation to Change the site URL:

  • Edit the Grav configuration file in www/ver1/user/config/system.yaml:
  • Edit the line custom_base_url: to match the subfolder (note: don’t change the rest of the lines which I just represent as ellipsis below).
  • And similarly, edit the session: > path:, making sure path is preceded by exactly two spaces!
  • Make sure asolute_urls is false, which is should be by default.
  • And, if you still have RewriteBase /ver1 or similar in .htaccess, then delete the line or comment it out with #.
  • Save and test.
absolute_urls: false
custom_base_url: 'http://localhost/ver1'
...
session:
  ...
  path: '/ver1'

Hope it helps.

BTW added 28 Jul 2020 If you encounter an error that the required PHP module Zip is missing, try this workaround.