【问题标题】:Apache web server doesn't allow me to refresh on /about but on localhost its working fineApache Web 服务器不允许我在 /about 上刷新,但在 localhost 上它工作正常
【发布时间】:2015-11-16 00:12:18
【问题描述】:

我捆绑了我的一个项目,它运行良好。但是,当在路由 /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 .htaccess reactjs digital-ocean react-router


    【解决方案1】:

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

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

    这有时在不同的网络服务器中被称为“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 未找到。

    【讨论】:

    • 我在哪里写这个规则?我是否还必须在终端中输入任何命令才能激活重写?
    • 没关系,把它添加到虚拟主机下的 webconfig 文件中,谢谢。
    • 在 httpd.config 下我相信它有 只需在下面添加 Mike 所说的内容即可切换到 HTML5 模式。
    • 这也可以添加到与index.html 文件位于同一文件夹中的.htaccess 文件中,但您可能希望将其包装在&lt;IfModule mod_rewrite.c&gt;&lt;/IfModule&gt; 标记中
    • 我尝试了上述解决方案,但无法正常工作。我在 htaccess 文件中添加了上面的代码。
    【解决方案2】:

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

    Route::get('{url}', function () { 返回视图(“欢迎”); })->where(['url' => '.*']);

    欢迎刀片文件是:

    <!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 文件中显示欢迎刀片及其各自的路线

    【讨论】:

      猜你喜欢
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多