一、前言
前端Vue router 使用history模式,URL會比hash模式好看,這種模式要玩好,還需要后端配置支持,否則會報404錯誤。
注:1.前端代碼省略。
2.此處后台使用Apache服務支持。
二、后端配置部署
1.創建一個txt文件,在其中添加如下配置
參照官方文檔
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </IfModule>
更改文件名為: .htaccess (前面有個點)
把該文件放到和index.html同級
2.設置apache支持mod_rewrite
找到apache安裝目錄 etc/httpd/conf/ 下的 httpd.conf 文件
找到“LoadModule rewrite_module modules/mod_rewrite.so”,將前面的"#"號刪除。
(如果沒有找到,則新增一行,必須獨占一行)
3.設置apache服務支持.htaccess
修改httpd.conf -> "AllowOverride None" 為 "AllowOverride All"
4.重啟apache
命令:service httpd restart