Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to setup a htaccess to a subfolder

I have this .htaccess right now:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]

It works if i setup my script on root folder, but if i create a new folder called 'api' and move all my script to there, does not work as expected and i see a blank page.

I modified the .htacess like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

DirectoryIndex index.php
RewriteEngine on
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/index.php [QSA]

>Solution :

Unfortunately, it is not entirely clear from the config, but there are two different path mechanisms:

RewriteBase /api/

This is "relative" to the request (or browser). i.e. https://example.com/api/

However…

RewriteRule ^(.*)$ /api/index.php [QSA]

That is ‘relative’ to the server. So inside the server, you would need to have literally that file in a root directory called ‘api’.

In short, I believe that what you need is:

RewriteRule ^(.*)$ api/index.php [QSA]

(note the missing starting slash – which means path relative to the document root and not absolute server path)


On a side-note, with RewriteBase /api/, it effectively means you do not care about other (undefined) root paths (e.g. /api2/xx won’t be matched). Therefore there may be an even easier way to your original problem: just move your original .htaccess file into the api directory.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading