前后端同一台服务器配置:
- 所有搜索引擎的请求,交由后端处理。
- 所有404 (文件不存在)路由 交由index.html 前端处理。
- 所有API 相关交由 后端处理
nginx
location ^(?api|admin) {
if ($http_user_agent ~* "bot|spider|Bot|Spider"){
try_files $uri $uri/ /boot.php?$query_string;
}
try_files $uri $uri/ /index.html =404;
}
location ^(api|admin) {
try_files $uri $uri/ /boot.php?$query_string;
}
caddy
0.0.0.0:80 {
root ./demo
fastcgi / 127.0.0.1:9000 php
rewrite {
if {>Header} has bot
if {>Header} has spider
if_op or
to /boot.php
}
rewrite {
if {path} not_starts_with /api
# r ^/api
to {path} {path}/ /index.html
}
rewrite {
if {path} starts_with /api
# r ^/api
to /boot.php
}
}
Apache
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_USER_AGENT} *(bot|spider|Bot|Spider) boot.php [L]
RewriteRule ^(api|admin) boot.php [L]
RewriteRule ^(?api|admin) index.html [L]