Polymer 与 Apache 服务器:URL 和应用程序路由配置

Polymer with Apache server: URL and app-route configuration

我制作了一个小型 Polymer 移动应用程序。我将 build/bundled 文件夹(部署文件)复制到带有 Ubuntu 和 apache2.

的机器上

根是通常的var/www/html。有一个 index.html 文件,然后是一个 /src 文件夹,其中包含使用 Polymer 构建的所有其他界面 (htmls)。

目录的安全模型:

<Directory /var/www/>
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

在 Polymer 上,我在大多数页面上配置了应用程序路由,如下所示:

<app-location route="{{route}}"></app-location>
<app-route
    route="{{route}}"
    pattern="/:page"
    data="{{routeData}}"
    tail="{{subroute}}"></app-route>

有些页面是这样的:

<app-route route="{{route}}"
           pattern="/:recordid"
           data="{{routeData}}"></app-route>

这只是使用 Polymer 的基本页面路由,寻找要显示的页面名称。所以它的导航看起来像这样:

domain.com
domain.com/vehicles
domain.com/vehicles/id_of_the_vehicle
domain.com/customers

除了第一个(index.html),所有其他页面都在 /src 文件夹中。

一切正常,直到我刷新其中一个页面。例如,Chrome 上的 'pull-down-to-refresh' 功能会自动刷新页面。当刷新发生时,服务器会尝试查找“/vehicles”文件,例如,它会显示 404 错误,因为根文件夹中没有这样的文件。这是在 Polymer 的 app-route 组件上使用的路由,用于指示要查找的文件...

所以我想我需要将每个请求都视为 'index.html',因为这个文件指向另一个具有所有应用程序路由配置的文件 (my-app.html)...客户端应该处理所有的路线。这是正确的吗?我怎样才能做到这一点?我应该尝试使用 Node.js 的 Express 作为网络服务器而不是 apache 吗?

如果我 运行 使用本地 Polymer 服务器 (polymer serve --params) 的应用程序用于测试目的,它一切正常,刷新和所有。

正如我所怀疑的那样,我必须将每个请求(除了根'/')都视为index.html

所以根文件夹上的一个简单的 .htaccess 解决了它:

<IfModule mod_rewrite.c>
        Options +FollowSymLinks
        IndexIgnore */*
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule (.*) index.html
</IfModule>

如果您使用的是虚拟主机,则可能必须在重写条件前加上文档根目录,如下所示:

<ifModule mod_rewrite.c>
  # debug with apache since 2.4 mod_rewrite
  # LogLevel alert rewrite:trace6

  Options +FollowSymLinks
  IndexIgnore */*
  RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
  # last rule, append query string
  RewriteRule (.*) /index.html [L,QSA]
</ifModule>