Home > Mobile >  Redirect api routes to a different subfolder
Redirect api routes to a different subfolder

Time:06-24

My server has a frontend and a backend like this:

·
|__www
  |__frontend
     |__public
  |__backend
     |__public

And I would like to make all my routes point to the frontend except anything that has /api prefix.

For example:

domain.com/foo points to the frontend  
domain.com/api/foo points to the backend

I've tried with apache vhost and also with .htaccess

RewriteEngine On

RewriteRule ^api(/.*)?$ /backend/public$1 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond ^ /frontend/public [L]

How can I make it work?

CodePudding user response:

You'd need a rule to avoid both folders (frontend and backend) from being rewritten as well. Also, a RewriteRule is missing in your last rule.

RewriteEngine On

# Don't rewrite frontend nor backend
RewriteRule ^(frontend|backend)(/.*)?$ - [L,NC]

# /api/foo to /backend/public/foo
RewriteRule ^api(/.*)?$ /backend/public$1 [L,NC]

# not a file, /foo to /frontend/public/foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /frontend/public/$1 [L]
  • Related