Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim 3 - remove subdir from url with rewrite rule

I have a shared hosting on A2Hosting and I recently moved my main domain from public_html/ to public_html/subdir/

Here's the structure:

/public_html
 /subdir(site files of main domain)
  /api
  index.php  

My current htaccess(public_html) is :

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
RewriteRule ^(.*)$ /subdir/$1 [L]

When I called my APi before it was : domain.com/api/

But now it's : domain.com/subdir/api/

My htaccess in api is :

RewriteEngine On
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

How to remove the "subdir" in the url to keep my api like previous ? But still point the root to my subdir with my current htaccess ?

Thanks

like image 792
Jean R. Avatar asked May 29 '18 21:05

Jean R.


1 Answers

One way is to rewrite /api from the main .htaccess file in the root directory like this:

RewriteEngine On
RewriteBase /
RewriteRule ^api/(.*)$ ../subdir/api/$1 [R,L,QSA]

The .htaccess file in the new /subdir/api folder can stays the way it is. Only the RewriteBase needs to be adjusted.

Another way is to rewrite from the previous /api folder if you preserved it for some reason:

RewriteEngine On
RewriteBase /api
RewriteRule ^(.*)$ ../subdir/api/$1 [L]
like image 194
wp78de Avatar answered Nov 11 '22 20:11

wp78de