vhosts conf for single page app

Cat*_*hal 7 apache angularjs single-page-application

我有一个角度应用程序,我在本地使用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/$1 [L]
  </Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

如果我去应用程序的根目录,一切都很好.我在应用程序中进行了一些导航,这使我进入my.app.com/some/location并且仍然没有问题.

但是,如果我在此时重新加载页面,则会出现404错误.要回到我想要的地步,我需要回到root并再次通过应用程序导航,或者通过url my.app.com/#/some/location搜索

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

RewriteRule ^(.*)$ index.html/#/$1 [L]
Run Code Online (Sandbox Code Playgroud)

任何想法都非常感谢C.

Cat*_*hal 27

问题是我使用的是更新版本的apache,配置规则已经改变了.我更新的vhost现在读取

<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>
Run Code Online (Sandbox Code Playgroud)

  • 感到惊讶的是,该票没有更多的票,这是Google上“ apache单页应用”的第一个结果……谢谢! (2认同)