Apache Web 服务器不允许我刷新/about 但在 localhost 上它工作正常 [英] Apache web server doesn't allow me to refresh on /about but on localhost its working fine
问题描述
我捆绑了我的一个项目,它运行良好.但是,当在路由/about 上刷新时,它会显示在此服务器上找不到请求的 URL/about .. 但是,当我在我的本地主机上关闭 Web 服务器时,它在刷新和前进/后退按钮上工作正常.我正在使用 react-router 进行客户端路由.
I bundled up one of my projects and it works fine. However when hitting refresh on a route /about, it displays The requested URL /about was not found on this server.. However when I do it on my localhost off a web server it works fine on refresh and forward/back buttons. I'm using react-router for my client side routing.
这里是客户端路由,但我怀疑它的问题
Router.run(routes, Router.HistoryLocation, function (Handler) {
React.render(<Handler/>, app);
});
我的路线就在那里:
let routes = (
<Route>
<Route name = "App" path="/" handler = {App}>
<Route name="About" path="/about" handler = {About}/>
<DefaultRoute name="Projects" handler = {Projects}/>
</Route>
</Route>
);
这是我认为我破坏的 APACHE:
<Directory /var/www/>
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
Require all granted
#RedirectMatch ^/$ /apache2-default/
</Directory>
kkotwal.me.conf:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#
ServerName kkotwal.me
ServerAlias www.kkotwal.me
ServerAdmin webmaster@localhost
DocumentRoot /var/www/kkotwal.me/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
推荐答案
嘿,这其实是很常见的事情.
Hey this is actually a pretty common thing.
发生的事情是你需要让你的 apache 服务器忽略任何嵌套的路径,而是将所有请求 /*
发送到 root.这样你的前端 javascript 就可以在客户端获取路由并显示正确的视图.
What's happening is you need to get your apache server to ignore any nested paths and just send all requests /*
to root instead. That way your front-end javascript can pick up the route on the client-side and display the correct view.
这有时在不同的网络服务器中被称为HTML5 模式".
This is sometimes referred to as "HTML5 Mode" in different webservers.
在 apache 中,您这样做的方法是添加如下规则:
In apache the way you do this is add a rule like the following:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html [L]
这样做是告诉 Apache 提供任何存在的文件,但如果它们不存在,则只提供 /index.html
而不是 404 not found.
What this does is to tell Apache to serve any files that exist, but if they dont exist, just serve /index.html
rather than a 404 not found.
这篇关于Apache Web 服务器不允许我刷新/about 但在 localhost 上它工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!