小编典典

重写与Angular JS一起使用的Apache 2的规则

angularjs

显然,整个网络上都有大量的mod重写讨论和答案。但是,我很难抓住它们。所以我想我会问这里。

这是我当前在example.com根目录下的目录结构

  • 应用/
  • app / index.html(Angular js应用程序的“根”)
  • api(这将是一个用于’api’的symfony 2应用程序。我将从这里从angular发送ajax请求。)

我想将所有请求重定向到app / index.html,但对/ api的请求除外。

例如:

http://example.com/categories/electronics/ipod实际上就像去http://example.com/app/index.html/categories/electronics/ipod

我希望app / index.html部分被隐藏。

然后,对http://example.com/api的请求将出现异常,因为我将需要向这些url路径发出ajax请求。

感谢您提供的所有帮助/指导。


阅读 374

收藏
2020-07-04

共1个答案

小编典典

这可以帮助您(将其放入/.htaccess文件中):

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/api

# otherwise forward it to index.html 
RewriteRule ^.*$ - [NC,L]
RewriteRule ^app/. /app/index.html [NC,L]

注意: 对于较新的Apache版本,另请参阅下一个答案,该答案使用起来更容易FallbackResource

2020-07-04