404 symfony 5 API 和 Vuejs 作为不同根文件夹中的前端
404 with symfony 5 API and Vuejs as frontend in different root folders
我搜索了很多,但没有找到答案。这是我共享主机中的文件夹结构:
- api.subdomain
- index.php
- .htaccess
- ...
- public_html
- index.html
- ...
所以当我去 http://example.com it loads Vuejs and my api is in http://api.example.com
问题是,当我重新加载页面时,即 http://example.com/news(或手动输入 link),它会显示 404 未找到。我在 Vuejs 文档中读到:
Since our app is a single page client side app, without a proper server configuration, the users will get a 404 error if they access http://oursite.com/user/id directly in their browser.
他们让这个配置:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
但是 symfony 自动生成了这个:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::[=13=] ^(/.+)/(.*)::$
RewriteRule .* - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
我不知道如何“合并”它们,我不明白如果应用程序位于不同的根目录中,我该怎么办。我尝试了很多东西,但 none 有效
希望有人能帮助我。谢谢!
我已经修好了。如果其他人在共享主机中遇到此问题,解决方案是在 Vue 应用程序的根目录中添加一个 .htaccess(如 Vue 文档所述)(在我的例子中是 public_html)。
以防万一,这是 .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
我不知道这是否是最好的解决方案,但解决了问题。
我搜索了很多,但没有找到答案。这是我共享主机中的文件夹结构:
- api.subdomain
- index.php
- .htaccess
- ...
- public_html
- index.html
- ...
所以当我去 http://example.com it loads Vuejs and my api is in http://api.example.com
问题是,当我重新加载页面时,即 http://example.com/news(或手动输入 link),它会显示 404 未找到。我在 Vuejs 文档中读到:
Since our app is a single page client side app, without a proper server configuration, the users will get a 404 error if they access http://oursite.com/user/id directly in their browser.
他们让这个配置:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
但是 symfony 自动生成了这个:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::[=13=] ^(/.+)/(.*)::$
RewriteRule .* - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
我不知道如何“合并”它们,我不明白如果应用程序位于不同的根目录中,我该怎么办。我尝试了很多东西,但 none 有效
希望有人能帮助我。谢谢!
我已经修好了。如果其他人在共享主机中遇到此问题,解决方案是在 Vue 应用程序的根目录中添加一个 .htaccess(如 Vue 文档所述)(在我的例子中是 public_html)。
以防万一,这是 .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
我不知道这是否是最好的解决方案,但解决了问题。