简体   繁体   English

.htaccess用于HTML5模式的AngularJS app的子文件夹

[英].htaccess for subfolder of AngularJS app with HTML5 mode

Overview : 概述:

I have an AngularJS application that uses $locationProvider.html5Mode(true) and it is served from an Apache server. 我有一个使用$locationProvider.html5Mode(true)的AngularJS应用程序,它是从Apache服务器提供的。 Until now I used the source code to be accessed from the others and I just needed a redirect to index.html for the angularJS's HTML5 mode. 到目前为止,我使用了从其他人访问的源代码,我只需要为angularJS的HTML5模式重定向到index.html。 So I had this for the .htaccess file for my /app folder. 所以我把这个用于我的/app文件夹的.htaccess文件。

.htaccess : .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>

Modifications : 修改:

I then added Grunt for minifying the whole solution that is generated in the /dist folder of the app(so now the minified solution is found at /app/dist). 然后我添加了Grunt来缩小在应用程序的/dist文件夹中生成的整个解决方案(所以现在缩小的解决方案位于/ app / dist)。 I would now like to route the whole traffic of my app to the minified version, without making the users change the link. 我现在想将我的应用程序的整个流量路由到缩小版本,而不会让用户更改链接。 So, in practice I would like to redirect every call from /app to /app/dist. 所以,在实践中我想将每个来自/ app的调用重定向到/ app / dist。

Directory Structure now : 目录结构现在:

app
    dist
        minified versions(css,html,js)

I tried using some conditions from here in /app's .htaccess and moved the other .htaccess file in /app/dist, but with no success. 我尝试使用了一些条件,从这里在/ app的.htaccess和移动在/ app / DIST其他.htaccess文件,但没有成功。 I don't even know if the two can be combined in this way. 我甚至都不知道这两者是否能以这种方式结合起来。 I know I can use the dist folder as /app instead, but I wouldn't really want this, as /app is a repo and it is way easier to keep it updated. 我知道我可以使用dist文件夹作为/ app,但我不会真的想要这个,因为/ app是一个repo,它更容易保持更新。 So how can I manage to achieve this with redirecting? 那么如何通过重定向来实现这一目标呢?

Also, as a second thing, is it possible to put a flag somehow so I let alpha users use the normal (not-minified) version for better debugging? 另外,作为第二件事,是否有可能以某种方式放置标志,所以我让alpha用户使用普通(未缩小)版本来进行更好的调试?

Update: 更新:

I added a sample project for this here . 在这里添加了一个示例项目。 I just kept the initial .htaccess files that i showed above and you can check that both versions( /test and /test/dist ) work as stated above. 我只保留了上面显示的初始.htaccess文件,你可以检查两个版本( /test/test/dist )是否如上所述。 Also, it probably has some unused code and dependencies, but I just cut the other parts from my project(don't judge :D). 此外,它可能有一些未使用的代码和依赖项,但我只是从我的项目中删除其他部分(不要判断:D)。

For this I used the initial configuration with AngularJS 1.4.8 that could generate some errors like the one stated here and here . 为此,我使用了AngularJS 1.4.8的初始配置,可能会产生一些错误,如此此处所述 Feel free to update the AngularJS version to 1.5.8(the last stable release of Angular 1), but take in consideration the comments on Cosmin Ababei's post as well. 随意将AngularJS版本更新为1.5.8(Angular 1的最后一个稳定版本),但也要考虑对Cosmin Ababei帖子的评论。 Also, the project uses npm for installing grunt(and other extensions for grunt), bower for angular and some other basic components and grunt for building the minified version(actually, it's just concatenating resources in same files for now). 此外,该项目使用npm来安装grunt(和grunt的其他扩展),bower for angular和一些其他基本组件以及用于构建缩小版本的grunt(实际上,它现在只是在同一文件中连接资源)。 If you need to change the dependencies, please don't forget to run bower install . 如果您需要更改依赖项,请不要忘记运行bower install If you want to regenerate the dist folder, don't forget to run npm install and grunt build . 如果要重新生成dist文件夹,请不要忘记运行npm installgrunt build

I'll assume that inside the /dist directory you have index.html with the updated URLs which point to the minified resources and the correct <base> tag. 我假设在/dist目录中你有index.html其中包含更新的URL,这些URL指向缩小的资源和正确的<base>标记。 Note that the <base> tag must point to the /dist folder - just append /dist to the value you're using for the uncompressed index.html and you'd be fine. 请注意, <base>标记必须指向/dist文件夹 - 只需将/dist附加到您为未压缩的 index.html使用的值,您就可以了。

With those things in order, here's the updated .htaccess file: 按顺序排列这些东西,这是更新的.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]

You'll notice two things: 你会注意到两件事:

  • I've removed RewriteCond %{REQUEST_FILENAME} -d as most likely you don't need to serve whole directories 我删除了RewriteCond %{REQUEST_FILENAME} -d因为很可能你不需要提供整个目录

  • I've added the ALPHA_USER cookie which would redirect the users to the uncompressed files. 我添加了ALPHA_USER cookie,它会将用户重定向到未压缩的文件。 This cookie can have any value you desire. 这个cookie可以有你想要的任何价值。

Inside public_html => /app => .htaccess try this : 在public_html => / app => .htaccess里面试试这个:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ dist/index.html [L]

Note : HTML <base> tag should be point to the dist folder. 注意: HTML <base>标记应指向dist文件夹。 I hope this solution will work as per your expectations. 我希望这个解决方案能够按照您的期望工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM