概述 :
我有一个 AngularJS 应用程序,它使用$locationProvider.html5Mode(true)
它由 Apache 服务器提供。到目前为止,我使用的源代码是从其他人那里访问的,我只需要重定向到 index.html 以实现 AngularJS 的 HTML5 模式。所以我有这个.htaccess
为我的文件/app
文件夹。
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</IfModule>
修改:
然后我添加了 Grunt 来缩小在/dist
应用程序的文件夹(所以现在可以在 /app/dist 找到缩小的解决方案)。我现在想将我的应用程序的整个流量路由到缩小版本,而不会让用户更改链接。因此,实际上我想将每个调用从 /app 重定向到 /app/dist。
现在的目录结构:
app
dist
minified versions(css,html,js)
我尝试使用一些条件here https://stackoverflow.com/questions/990392/htaccess-rewrite-to-redirect-root-url-to-subdirectory在 /app 的 .htaccess 中并将另一个 .htaccess 文件移动到 /app/dist 中,但没有成功。我什至不知道这两者是否可以通过这种方式结合起来。我知道我可以使用 dist 文件夹作为 /app 来代替,但我真的不想这样做,因为 /app 是一个存储库,并且更容易保持更新。那么我怎样才能通过重定向来实现这一目标呢?
另外,第二件事是,是否可以以某种方式放置一个标志,以便我让 alpha 用户使用正常(未缩小)版本来更好地调试?
Update:
我为此添加了一个示例项目here https://github.com/radumvlad/angular-dist-redirect。我只保留了上面显示的初始 .htaccess 文件,您可以检查两个版本(/test
and /test/dist
) 如上所述工作。另外,它可能有一些未使用的代码和依赖项,但我只是从我的项目中删除了其他部分(不要判断:D)。
为此,我使用了 AngularJS 1.4.8 的初始配置,这可能会生成一些错误,如所述错误here https://stackoverflow.com/questions/29261782/ and here http://github.com/angular/angular.js/issues/11091。请随意将 AngularJS 版本更新到 1.5.8(Angular 1 的最后一个稳定版本),但请考虑以下评论科斯明·阿巴贝的帖子 https://stackoverflow.com/a/39218569/1787172以及。此外,该项目使用 npm 来安装 grunt(以及 grunt 的其他扩展),使用 Bower 来安装 Angular 和其他一些基本组件,并使用 grunt 来构建缩小版本(实际上,现在只是将资源连接到同一文件中)。如果您需要更改依赖项,请不要忘记运行bower install
。如果您想重新生成 dist 文件夹,请不要忘记运行npm install
and grunt build
.