SPA里面必须要带有#,才能正确识别路径。
如果要去掉#,就要开启history mode。
开启后会有一个问题,比如直接访问或者刷新domain.com/myprofile的时候,浏览器回去找域名下的myprofile文件夹,但其实是不存在的,所以要用rewrite规则来解决,让访问这个路径的时候,不要去找文件夹
配置两步,第一步在根目录下新建.htaccess文件,粘贴如下内容:
RewriteEngine On
# 1. /api/ 不重写
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule ^ - [L]
# 2. 静态资源不重写
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 3. 其它全部重写到 index.html
RewriteRule . /index.html [L]
第二步,在httpd-vhosts.conf文件里,允许重写,也就是AllowOverride All
<VirtualHost _default_:80>
ServerName mytest.com
DocumentRoot "D:/dist/"
<Directory "D:/dist/">
Options +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
# 代理 /api 到后端
ProxyPass /api/ http://localhost:3000/api/
ProxyPassReverse /api/ http://localhost:3000/api/
</VirtualHost>
最新回复