Rad*_*lad 11 apache .htaccess mod-rewrite angularjs html5mode
概述:
我有一个使用的AngularJS应用程序,$locationProvider.html5Mode(true)它是从Apache服务器提供的.到目前为止,我使用了从其他人访问的源代码,我只需要为angularJS的HTML5模式重定向到index.html.所以我.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>
Run Code Online (Sandbox Code Playgroud)
修改:
然后我添加了Grunt来缩小在/dist应用程序文件夹中生成的整个解决方案(所以现在缩小的解决方案位于/ app/dist).我现在想将我的应用程序的整个流量路由到缩小版本,而不会让用户更改链接.所以,在实践中我想将每个来自/ app的调用重定向到/ app/dist.
目录结构现在:
app
dist
minified versions(css,html,js)
Run Code Online (Sandbox Code Playgroud)
我尝试使用了一些条件,从这里在/ app的.htaccess和移动在/ app/DIST其他.htaccess文件,但没有成功.我甚至不知道这两者是否可以这样组合.我知道我可以使用dist文件夹作为/ app,但我不会真的想要这个,因为/ app是一个repo,它更容易保持更新.那么如何通过重定向来实现这一目标呢?
另外,作为第二件事,是否有可能以某种方式放置标志,所以我让alpha用户使用普通(未缩小)版本来进行更好的调试?
更新:
我在这里添加了一个示例项目.我只保留了上面显示的初始.htaccess文件,您可以检查两个版本(/test和/test/dist)是否如上所述.此外,它可能有一些未使用的代码和依赖项,但我只是从我的项目中删除其他部分(不要判断:D).
为此,我使用了AngularJS 1.4.8的初始配置,可能会产生一些错误,如此处和此处所述.随意将AngularJS版本更新为1.5.8(Angular 1的最后一个稳定版本),但也要考虑对Cosmin Ababei帖子的评论.此外,该项目使用npm来安装grunt(和grunt的其他扩展),bower for angular和一些其他基本组件以及用于构建缩小版本的grunt(实际上,它现在只是在同一文件中连接资源).如果您需要更改依赖项,请不要忘记运行bower install.如果要重新生成dist文件夹,请不要忘记运行npm install和grunt build.
我假设/dist目录内有index.html更新的 URL,它们指向缩小的资源和正确的<base>标签。请注意,标签<base>必须指向/dist文件夹- 只需附加/dist到您用于未压缩的 index.html值就可以了。
按顺序排列这些内容后,这是更新后的.htaccess文件:
RewriteEngine On
# Don't rewrite files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# If the ALPHA_USER cookie is set, serve the uncompressed files
RewriteCond %{HTTP_COOKIE} ALPHA_USER [NC]
RewriteRule ^ index.html [L]
# Rewrite everything
RewriteRule ^ dist/index.html [L]
Run Code Online (Sandbox Code Playgroud)
你会注意到两件事:
我已经删除了,RewriteCond %{REQUEST_FILENAME} -d因为很可能您不需要提供整个目录
我添加了ALPHA_USERcookie,它将用户重定向到未压缩的文件。该 cookie 可以具有您想要的任何值。
| 归档时间: |
|
| 查看次数: |
4610 次 |
| 最近记录: |