单页应用程序的 vhosts conf

vhosts conf for single page app

我有一个 angular 应用程序,我在本地使用 apache 提供服务。我的 vhosts.conf 是:

<VirtualHost *:80>
  ServerName my.app.com
  DirectoryIndex index.html
  DocumentRoot /export/www/app
  <Directory "/export/www/app">
    order allow,deny
    allow from all

    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.html/ [L]
  </Directory>
</VirtualHost>

如果我转到应用程序的根目录,一切都会正常加载。我在应用程序中进行了一些导航,将我带到 my.app。com/some/location 仍然没问题。

但是,如果我此时重新加载页面,则会收到 404 错误。要回到我想要的位置,我需要返回根目录并再次通过应用程序导航,或者通过 url my.app.com/#/some/location[=12= 进行搜索]

我可以用重写规则做些什么,这样我就不需要哈希来重新加载路由了吗?我尝试将现有规则编辑为:

RewriteRule ^(.*)$ index.html/#/ [L]

非常感谢任何想法 C

奇怪的是 my.app.com/some/location 有效。在 AcceptPAthInfo ON 的情况下,httpd 将服务 index.html 并传递 /some/location 的 PATH_INFO 但这不应该在客户端 JS 上做正确的事情。

您尝试的重写似乎应该完全取代您现有的重写。但是,您希望显式重定向到非相对 /index.html 并添加 [R] 标志以确保重定向一直到客户端。否则,它只是一个内部替换,你需要你的浏览器来读取#,因为它只在客户端处理。

问题是我使用的是配置规则已更改的较新版本的 apache。我更新后的虚拟主机现在显示为

<VirtualHost *:80>
  ServerName my.app.com
  DirectoryIndex index.html
  DocumentRoot /export/www/app
  <Directory "/export/www/app">
    order allow,deny
    allow from all

    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
  </Directory>
</VirtualHost>