I have a file structure that looks like this
demo
| build
| | images
| | |- logo.png
| | static
| | css
| | |- style.css
| | js
| |- index.html
| controllers
| api
|- users.php
Here's a map that how I want to setup my apache vhosts.
- All the normal paths need to be redirected to
build/index.html
- Resource paths(ends with
.*
) need to be redirected tobuild/**
and keep the file path - APIs(starts with
/api/**
) need to be redirected tocontrollers/api/**
url | path |
---|---|
https://demo.com/ |
build/index.html |
https://demo.com/users |
build/index.html |
https://demo.com/users/16 |
build/index.html |
https://demo.com/images/logo.png |
build/images/logo.png |
https://demo.com/static/css/style.css |
build/static/css/style.css |
https://demo.com/api/users |
controllers/api/users |
Edit
After a lot of attempts I created the apache vhost like this and it kinda works. Is it the right way to do it? I don't have much experience on server-side.
Now I have to achieve the same with nginx since my real server is using nginx. How do I do the same with nginx settings?
<VirtualHost *:80>
DocumentRoot "/Users/hao/Projects/demo"
ServerName demo.dev
Options FollowSymLinks
RewriteEngine on
RewriteRule ^(?!/?api/)(.*\.\w )$ /build/$1 [L]
RewriteRule ^(?!/?api/|/?$) /build/index.html [L]
RewriteRule ^/?api/(.*)$ /controllers/api/$1 [QSA,NE,L]
</VirtualHost>
CodePudding user response:
Looks like you can just use /Users/hao/Projects/demo/build
as your root. Assuming you have only PHP files under /Users/hao/Projects/demo/controllers/api
directory, you can try the following configuration:
server {
server_name demo.dev;
root /Users/hao/Projects/demo/build;
index index.html;
location / {
try_files $uri /index.html;
}
location ~ ^/api/. \.php$ {
root /Users/hao/Projects/demo/controllers;
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass <socket path or ip:port to PHP-FPM daemon here>;
}
}