javascript - Route does not work on live server but it work on localhost
问题描述
What is wrong with this code. It works on localhost but it doesn't work on live Apache server. I already specify the homepage on package json and also have an htaccess. It only blank on live server.
app.js:
import React from 'react'
import {
Route,
Switch,
Redirect,
BrowserRouter as Router
} from 'react-router-dom'
import Login from './component/login/index'
function App() {
return (
<Router>
<Switch>
<Route exact path="/" component={Login} />
<Route path='/login' component={Login} />
</Switch>
</Router>
);
}
export default App;
component index.js:
import React from 'react'
export default function Login(props) {
return (
<div>Login</div>
)
}
htaccess:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
Dependencies:
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
},
解决方案
试试这个.htaccess
,
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt|svg|woff|ttf|eot)$
RewriteRule . index.html [L]
推荐阅读
- loops - SAS 迭代是如何通过变量 _N_ 工作的,如果有的话
- python - 在python中将几个变量阈值成二进制分类代码
- c++ - 为什么不对指针强制执行 constness?
- php - 在自定义 magento 2 模块中使用外部 php 库的最简单方法是什么?
- node.js - TrueVault BLOB 到节点中的文件
- html - 如何对 srcset 中的不同图像使用不同的 alt?
- sql-server - 按添加日期排序和过滤,同时在 Id 行上的 CLUSTERED IDENTITY PRIMARY KEY
- javascript - 我如何使用 vue.js 格式化文本
- ios - 如何让手电筒闪烁?
- angular - Angular Form根据下拉值添加输入字段