【发布时间】:2022-10-31 05:20:48
【问题描述】:
我在 Laravel 应用程序的公共目录中托管了一个 React 应用程序,其中:
/public_html/
app
bootstrap
config
database
public <--- React App Here
static <--- Belongs to React
index.html <--- Belongs to React
index.php <--- Belongs to Laravel
.htaccess <--- htaccess (2)
.htaccess <--- htaccess (1)
other laravel folders
.
.
.
根目录的.htaccess htaccess(1) 内容如下,它负责从 Laravel 应用的路由中移除 /public:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
Laravel 的公用文件夹.htaccesshtaccess(2) 内容如下,我没碰过:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
问题是当我通过https://myapp.com 访问应用程序时,React 会控制路由,并将我重定向到https://myapp.com/home 视图,而如果我刷新页面,Laravel 的路由会控制并显示@987654329 的 Laravel 404 错误@路由不存在。
如何修复 .htaccess 文件以忽略 React 的路由并仅加载 index.html 文件,同时保持 Laravel 的 /api 路由正常工作?由于所有 React 应用程序路由都使用 Laravel 的 API 控制器。
【问题讨论】:
标签: reactjs laravel .htaccess react-router