Apache网络服务器不允许我在/about页面刷新,但在本地主机上可以正常工作。

29

我打包了我的一个项目,它运行良好。但是当在路由/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>
2个回答

70

嘿,这其实是一个相当常见的问题。

发生的情况是你需要让你的apache服务器忽略任何嵌套路径,并将所有请求/*发送到根目录。这样,你的前端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未找到。


2
我应该在哪里写这个规则?我是否需要在终端中输入任何命令来激活重写功能? - Karan
1
没事了,我把它添加到了webconfig文件中,在虚拟主机下面,谢谢。 - Karan
在httpd.config文件中,我相信它有<directory var/...>,只需在此下面添加Mike在这里提到的内容即可切换到HTML5模式。 - Karan
3
这也可以加入到与index.html文件在同一文件夹中的 .htaccess 文件中,但你可能希望将其包装在一个 <IfModule mod_rewrite.c></IfModule> 标签中。 - aviemet
2
我尝试了上面的解决方案,但并没有起作用。我已经将上述代码添加到了htaccess文件中。 - Abhishek
显示剩余4条评论

0
在 Laravel 中,您需要呈现相同的视图,其中所有路由都写在 React 文件中。Laravel 路由的编写方式如下:
Route::get('{url}', function () { return view('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> 

因此,每次在about路由上刷新时,它都会显示欢迎刀片和其在app.js文件中相应的路径。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接