用于角度js的apache 2的重写规则

Mik*_*aas 21 apache mod-rewrite rewrite angularjs

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

我要求重写规则来做Andy Joslin在评论中解释的内容:https://stackoverflow.com/a/11100438

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

  • 应用程序/
  • app/index.html(角度js应用程序的"根")
  • api(这将是一个用于'api'的symfony 2应用程序.我将从角度向这里发送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请求.

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

Sco*_*son 24

这个问题的公认答案已经过时了.您现在可以在Apache 2.2.16+的conf文件中使用FallbackResource指令.

FallbackResource /app/index.html
Run Code Online (Sandbox Code Playgroud)

如果您希望FallbackResource指令忽略"/ api"路由:

<Directory /api>
FallbackResource disabled
</Directory> 
Run Code Online (Sandbox Code Playgroud)

  • 酷指令.但是,如何在忽略对"/ api"的请求时使用它?这是我最初的问题. (2认同)
  • 它可以设置为专门忽略/ api路由.我编辑了答案以反映这一点. (2认同)

Ste*_*wie 15

这里有一些东西让你去(把它放在你的/.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]
Run Code Online (Sandbox Code Playgroud)