小编典典

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

reactjs

我捆绑了一个项目,效果很好。但是,当在路由/ 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>

阅读 292

收藏
2020-07-22

共1个答案

小编典典

嘿,这实际上是一件很普通的事情。

发生的事情是,您需要使apache服务器忽略任何嵌套的路径,而只是将所有请求发送/*到root。这样,您的前端javascript可以在客户端选择路由并显示正确的视图。

有时在不同的Web服务器中将其称为“ 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。

2020-07-22