a.l.e
a.l.e

Reputation: 868

htaccess rewrite for routing from a sub directory

I've been using this .htaccess file for enabling the grav Cms for a few sites:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/grav/
RewriteRule ^$ /grav [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /grav/$1 [L,QSA]

This works flawlessly as long the site is at the document root.

Now, I have a new site that starts in 2020/ and has Grav installed in 2020/grav.

I modified the above .htaccess to be:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/2020/grav/
RewriteRule ^2020$ /2020/grav [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^2020/(.*)$ /2020/grav/$1 [L,QSA]

This .htaccess file is located in /2020/.

While in the first case the url is left as is and Grav gets the rest of the url as parameters, in the second case (2020/) the url changes to /2020/grav, which is not what I want?

I wanted to check if it was a Grav issue and I've setup a simple test case, with the above .htaccess file and a grav/ directory with only an index.php file in there and I got the same result.

Any hint on how I have to modify the .htaccess file to get 2020/abc to stay as is in the url bar and at the same time have Grav to get the abc argument?

Upvotes: 1

Views: 234

Answers (2)

a.l.e
a.l.e

Reputation: 868

The original question was about getting it to work with an .htaccess.

@anubhava gave a very good answer for the general case. But it sadly was not enough for Grav.

I got some more help in the Grav forums and at the end of a long journey Ole found out that the question has been asked in their forums a long time ago and a solution was found!

The /2020/.haccess can be simply defined as:

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

But what one need is to add a couple of configurations in the /2020/grav/user/config/system.yaml file:

custom_base_url: 'http://domain.tld/2020'
session:
  path: /2020

You can find a more detailed question and Ole's reply in the Grav forums

Upvotes: 0

anubhava
anubhava

Reputation: 785541

You can have this code in 2020/.htaccess:

RewriteEngine On

RewriteRule ^$ grav/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!grave/)(.+)$ grav/$1 [L,NC]

(?!grave/) is negative lookahead condition that will skip match if match already starts with grave/

Upvotes: 1

Related Questions