在Angular中,如何使用.htaccess忽略文件 [英] In Angular, how to ignore a file using .htaccess

查看:208
本文介绍了在Angular中,如何使用.htaccess忽略文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular9.x。我需要忽略根目录中的特定文件。例如:

I am using Angular 9.x. I need to ignore a specific file in the root directory. For example:

/root
   - index.html
   - ignorefile.json

我尝试向.htaccess添加重写规则,但没有成功。

I tried adding a rewrite rule to .htaccess with no success.

RewriteEngine On
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_URI} !^/ignorefile\.json$ [NC] # this does not work
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.html [L]

编译应用程序时如何忽略特定文件?

How can I ignore a specific file when compiling the app?

推荐答案

INFO

Angular不会生成& ;处理服务器端重定向。

Angular does not generate & handle server-side redirections.

要在Angular中从一个视图导航到下一个视图,请使用Angular路由器。路由器通过将浏览器URL解释为更改视图的指令来启用导航。

To handle the navigation in Angular from one view to the next, you use the Angular router. The router enables navigation by interpreting a browser URL as an instruction to change the view.

当路由器导航到新的组件视图时,它将通过以下方式更新浏览器的位置和历史记录:该视图的URL。由于这是严格的本地URL,因此浏览器不会将此URL发送到服务器,也不会重新加载页面。

When the router navigates to a new component view, it updates the browser's location and history with a URL for that view. As this is a strictly local URL the browser won't send this URL to the server and will not reload the page.

如果您使用的是(默认为Angular 9)适用于现代浏览器的PathLocationStrategy,它们支持history.pushState,这是一种更改浏览器的位置和历史记录而无需触发服务器页面请求的技术。路由器可以组成一个自然 URL,该URL与需要页面加载的URL不能区分。

If you are using the (Angular 9 default) PathLocationStrategy for modern browsers, they support history.pushState, a technique that changes a browser's location and history without triggering a server page request. The router can compose a "natural" URL that is indistinguishable from one that would otherwise require a page load.

在这种情况下,服务器需要将所有流量重定向到索引.html,以便Angular可以接管UI上的路径。为此,您必须将.htaccess文件附加到生产服务器上才能使路由工作。

In this case, the server needs to redirect all traffic to index.html so Angular can take over to handle path on UI. For this, you have to append a .htaccess file to your production server in order to make routing work.

ANSWER

如果您尝试与您一起提供ignorefile.json角度的应用程序,这是完全有可能的。您应该更新您的angular.json文件。

If you are trying to serve ignorefile.json along with your angular app, it is quite possible. You should update your angular.json file.

   "assets": [
     ...
     {
       "glob": "ignorefile.json",
       "input": "src/",
       "output": "/"
     },
     ...
   ]

foo.com将加载Angular应用程序,但foo.com/ignorefile .json将显示JSON数据。

foo.com will load the Angular app, but foo.com/ignorefile.json will show the JSON data.

选中此答案; https://stackoverflow.com/a/61972932/2827820

这篇关于在Angular中,如何使用.htaccess忽略文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆