Apache Web 服务器不允许我在 /about 上刷新,但在本地主机上它工作正常

Apache web server doesn't allow me to refresh on /about but on localhost its working fine

我捆绑了我的一个项目,它运行良好。但是,当在路由 /about 上点击刷新时,它会显示 The requested URL /about was not found on this server.. 但是,当我在我的本地主机上关闭 Web 服务器时,它在刷新和 [=30 时工作正常=] 按钮。我正在为我的客户端路由使用 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 服务器忽略任何嵌套路径,而只将所有请求 /* 发送给根目录。这样你的前端 javascript 就可以在客户端获取路由并显示正确的视图。

这在不同的网络服务器中有时被称为 "HTML5 Mode"。

在 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 found。

在 laravel 的情况下,您需要呈现相同的视图,其中所有路由都为 react 文件编写。 Laravel路线写成:

Route::get('{url}', function () { return视图('welcome'); })->where(['url' => '.*']);

欢迎 blade 文件是:

<!doctype html> 
<html lang="{{ app()->getLocale() }}">
    <head>
         <meta charset="utf-8">
         <meta http-equiv="X-UA-Compatible" content="IE=edge">
         <meta name="viewport" content="width=device-width, initial-scale=1">
         <title>Laravel</title>
         <link href="{{asset('css/app.css')}}" rel="stylesheet" type="text/css">
    </head>
    <body>
         <div id="root"></div>
         <script src="{{asset('js/app.js')}}" ></script>
    </body> 
</html>

app.js 路径文件

 <Route>
     <Route name = "App" path="/" handler = {App}>
     <Route name="About" path="/about" handler = {About}/>
     <DefaultRoute name="Projects" handler = {Projects}/>
   </Route> 

所以每次您刷新关于路线时,它都会在 app.js 文件

中显示欢迎 blade 及其各自的路线