重写 apache 2 的规则以与 angular js 一起使用 [英] rewrite rules for apache 2 to use with angular js
问题描述
显然,网络上有很多关于模组重写的讨论和答案.但是,我很难掌握它们.所以我想我会在这里问.
Obviously, there are a lot of mod rewrite discussions and answers all across the web. However, I am having a hard time grasping them. So I thought I would ask here.
我要求重写规则来执行 Andy Joslin 在此处的评论中解释的内容:https://stackoverflow.com/a/11100438
I'm asking for rewrite rules to do what Andy Joslin explained in the comments here: https://stackoverflow.com/a/11100438
这是我当前在 example.com 根目录下的目录结构
This is my current dir structure at the root of example.com
- 应用程序/
- app/index.html(angular js 应用程序的根")
- api(这将是一个用于 'api' 的 symfony 2 应用程序.我将通过 angular 向这里和从这里发送 ajax 请求.)
我想将所有请求重定向到 app/index.html,除了对/api 的请求.
I would like to redirect all requests to app/index.html except for requests to /api.
例如:
http://example.com/categories/electronics/ipod 实际上就像去http://example.com/app/index.html/categories/electronics/iPod
我想隐藏 app/index.html 部分.
I would like for the app/index.html part to be hidden however.
然后,对 http://example.com/api 的请求会有一个例外,因为我会需要向这些 url 路径发出 ajax 请求.
Then, there would be an exception for requests to http://example.com/api because I will need to make ajax requests to those url paths.
感谢所有帮助/指导.
推荐答案
这里有一些东西可以帮助你(把它放在你的/.htaccess 文件中):
Here's something to get you going (put this inside your /.htaccess file):
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
NOTE: For newer Apache versions see also the next answer, which uses the much easier FallbackResource
这篇关于重写 apache 2 的规则以与 angular js 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!