gpt4 book ai didi

Apache Web 服务器不允许我刷新 on/about,但在 localhost 上它工作正常

转载 作者:行者123 更新时间:2023-12-03 13:02:27 24 4
gpt4 key购买 nike

我捆绑了我的一个项目,它运行良好。但是,当在路由/about 上点击刷新时,它会显示请求的 URL/about 在此服务器上找不到。但是,当我在 Web 服务器上的本地主机上执行此操作时,它在刷新和前进/后退按钮上工作正常。我正在使用 React-router 进行客户端路由。

这是客户端路由,但我怀疑这是问题

 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>

最佳答案

嘿,这实际上是一件很常见的事情。

发生的情况是,您需要让 apache 服务器忽略任何嵌套路径,而只是将所有请求 /* 发送到 root。这样你的前端 JavaScript 就可以在客户端获取路由并显示正确的 View 。

这有时在不同的网络服务器中称为“HTML5 模式”。

在 apache 中,执行此操作的方法是添加如下规则:

  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 find。

关于Apache Web 服务器不允许我刷新 on/about,但在 localhost 上它工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32150653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com