Grav in subfolder without site visitors ever knowing

I spent several hours today researching this and I can’t seem to find a definitive answer whether this is possible. I use Apache and I have Grav installed in a subfolder of webroot (/public_html/grav). I can access the Grav pages fine by going to www.mysite.com/grav/mypage. However, I would like to access the Grav pages by going to www.mysite.com/mypage.

Installing Grav into the webroot directly is not an option unfortunately due to a picky client who doesn’t want to mix his non-CMS based HTML files with the Grav files. He also doesn’t want to do a redirect from /mypage to /grav/mypage, but rather an internal rewrite that is invisible to the user.

I tried doing a simple rewrite rule in the htaccess file located in webroot, but I get Grav 404 errors that way. My rewrite rule looks like this: RewriteRule ^mypage/?$ /grav/index.php [NC,L]. I imagine Grav can’t figure out what page I’m trying to access, because the URL it gets doesn’t include the directory in which it’s installed.

I also played around with the setup.php file and tried to adapt the multisite setup for m y purpose, but I couldn’t get it working that way either.

Any ideas? Is this even possible to do?

Thank you for your help!

Gabe

Try this
RewriteEngine On
RewriteRule ^$ mypage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ mypage/$1

Thanks, but that gave me an internal server error. Are you sure that’s correct? Don’t I have to reference /grav/index.php somewhere to make sure Grav gets called?

@Gabe I’ve got this PR in place https://github.com/getgrav/grav/pull/896 it’s meant to work on a domain/subfolder -> domain level, but should also work in your case. Give it a try.

What about changing your dns target directory? Or moving Grav a folder higher?

So the rewrite rules I posted are correct…just need to change the system.yaml file. @flaviocopes mine works already without setting the system.yaml file. I was trying to get it to hide the subdomain in the url bar but didn’t have any luck.

@flaviocopes: thanks! I’ll give that a try.

@paulmassendari: not sure I can change the dns target directory due to the host being used. Moving Grav a folder higher would put it in webroot, which I’m trying to avoid.

@jmonroe: sorry to be a noob, but what changes do I have to make to system.yaml to get your rewrite rules to work? Looking through all of the settings there, it’s not obvious to me.

Thank you all!

As @flaviocopes stated in https://github.com/getgrav/grav/pull/896

By setting custom_base_url_absolute and custom_base_url_relative in system.yaml, we can have Grav in a subfolder but run it in the domain root.

Set

custom_base_url_absolute: 'http://localhost:8080
custom_base_url_relative: /

And in the domain root, set the redirect, e.g. with .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/grav-develop/
RewriteRule ^(.*)$ /grav-develop/$1

where grav-develop is the subfolder where Grav is.

Grav is expected to run in http://localhost:8080/grav-develop but with this configuration, it works in http://localhost:8080

Oh ok, I didn’t realize you were referring to that. I was running the stable version of Grav before, so now I’m upgrading to the RC and making those PR changes manually. Thanks for your help!

No problem. All I did was add my htaccess file and it worked. Looks like they added that to the github released version to make it easier to do what you’re doing. Let me know if it works or not.

Yup, it works!

Glad to hear it. Good job!

I may have celebrated too early. The frontend does work, but trying to log into the admin backend (I have the admin plugin installed) gives me an Invalid Security Token error. It’s almost like the admin plugin needs to be modified to use the custom_base_url_absolute as well, but I wouldn’t know where to do that or if that is really the problem. Any ideas or do any of you encounter this too? My htaccess file looks like this now:

RewriteRule ^admin(.)/?$ /grav/admin/$1 [NC,L]
RewriteRule ^user/(.
)/?$ /grav/user/$1 [NC,L] #to ensure css loads correctly in admin
RewriteRule ^system/(.)/?$ /grav/system/$1 [NC,L] #to ensure jquery loads correctly in admin
RewriteRule ^mycmshome(.
)/?$ /grav/mycmshome/$1 [NC,L]

So just to recap: if I comment out custom_base_url_absolute, the admin backend works fine at /grav/admin, but the frontend gives me a Grav 404 if I go to /mycmshome. Having custom_base_url_absolute set, the frontend works fine, but the admin backend gives me the Invalid Security Token error.

Thanks again for your help!

Never mind, I figured it out. In case anybody else runs into this problem, make sure that in your system.yaml file you have the following set:

custom_base_url_absolute: 'http://www.mysite.com'
custom_base_url_relative: /
absolute_urls: true

My mistake was leaving absolute_urls set to false, which resulted in the odd behavior I discribed before. Facepalm!

Awesome to hear!