Javascript 路由在实时服务器上不起作用,但在本地主机上起作用
这个代码有什么问题。它可以在本地主机上工作,但不能在实时Apache服务器上工作。我已经在包json上指定了主页,并且还有一个htaccess。它仅在live server上为空 app.js:Javascript 路由在实时服务器上不起作用,但在本地主机上起作用,javascript,reactjs,apache,Javascript,Reactjs,Apache,这个代码有什么问题。它可以在本地主机上工作,但不能在实时Apache服务器上工作。我已经在包json上指定了主页,并且还有一个htaccess。它仅在live server上为空 app.js: import React from 'react' import { Route, Switch, Redirect, BrowserRouter as Router } from 'react-router-dom' import Login from './component/l
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;
import React from 'react'
export default function Login(props) {
return (
<div>Login</div>
)
}
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
},
依赖关系:
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;
import React from 'react'
export default function Login(props) {
return (
<div>Login</div>
)
}
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
},
您可以将其添加到htaccess文件中,以将每个请求重定向到索引,但图像、css和js文件的请求除外
RewriteCond %{REQUEST_URI} !^/images [NC]
RewriteCond %{REQUEST_URI} !^/css [NC]
RewriteCond %{REQUEST_URI} !^/js [NC]
RewriteRule ^([^/]+)/?$ index.html [L]
您可以将其添加到htaccess文件中,以将每个请求重定向到索引,但图像、css和js文件的请求除外
RewriteCond %{REQUEST_URI} !^/images [NC]
RewriteCond %{REQUEST_URI} !^/css [NC]
RewriteCond %{REQUEST_URI} !^/js [NC]
RewriteRule ^([^/]+)/?$ index.html [L]
尝试使用此
.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]
尝试使用此
.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]
我在控制台里检查。它没有给出一个错误。静态文件的路径可以访问并正常工作,但在页面上为空,并且在组件登录时不显示文本。我怀疑路由有问题,因为当我创建了一个新的create react应用程序而没有路由时,它正常工作。我在控制台中检查。它没有给出一个错误。静态文件的路径可以访问并正常工作,但在页面上是空白的,并且在组件登录上没有显示文本。我怀疑路由有问题,因为当我创建了一个新的create react应用程序而没有路由时,它正常工作。你能共享你的htaccess文件吗?我现在就知道了。当我将它添加到每个路由路径“process.env.PUBLIC\uURL”时,它就可以工作了我不知道为什么,因为它可以在我的旧版本react应用程序中工作,而不在每个路由路径上指定proccess.env.PUBLIC_URL,只要我在包json和.htaccess上指定corect主页,但react的最新版本不工作。你能共享你的htaccess文件吗?我现在就知道了。当我将它添加到每个路由路径“process.env.PUBLIC\uURL”时,它就可以工作了我不知道为什么,因为它可以在我的旧版本react应用程序上运行,而不在每个路由路径上指定proccess.env.PUBLIC_URL,只要我在包json和.htaccess上指定corect主页,但最新版本的react不起作用。你可能需要根据自定义路径更新你的htaccess,如下所示:我现在就知道了。当我将它添加到每个路由路径“process.env.PUBLIC_URL”时,它就可以工作了。您可能需要根据自定义路径更新htaccess,如下所示:我现在就知道了。当我将它添加到每个路由路径“process.env.PUBLIC\uURL”时,它就可以工作了