Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular中,如何使用.htaccess忽略文件_Angular_.htaccess_Mod Rewrite - Fatal编程技术网

在Angular中,如何使用.htaccess忽略文件

在Angular中,如何使用.htaccess忽略文件,angular,.htaccess,mod-rewrite,Angular,.htaccess,Mod Rewrite,我使用的是Angular 9.x。我需要忽略根目录中的特定文件。例如: /root - index.html - ignorefile.json 我尝试向.htaccess添加重写规则,但没有成功 RewriteEngine On RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_URI} !^/ignorefile\.json$ [NC] # this does not work RewriteCond %{R

我使用的是Angular 9.x。我需要忽略根目录中的特定文件。例如:

/root
   - index.html
   - ignorefile.json
我尝试向.htaccess添加重写规则,但没有成功

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]

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

使用与index.html相同的规则,我建议添加
RewriteBase/

RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond ^ignorefile\.json$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.html [L]
如果它不起作用,那就严格一点

RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond ^ignorefile\.json$ - [END]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.html [L]
信息

Angular不生成和处理服务器端重定向

要处理从一个视图到下一个视图的角度导航,请使用角度路由器。路由器通过将浏览器URL解释为更改视图的指令来实现导航

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

如果您正在为现代浏览器使用(默认值为9)PathLocationStrategy,则它们支持history.pushState,这是一种在不触发服务器页面请求的情况下更改浏览器位置和历史的技术。路由器可以组成一个“自然”URL,它与需要加载页面的URL无法区分

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

回答

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

   "assets": [
     ...
     {
       "glob": "ignorefile.json",
       "input": "src/",
       "output": "/"
     },
     ...
   ]
foo.com将加载Angular应用程序,但foo.com/ignorefile.json将显示json数据


检查这个答案

Angular是一个前端框架,即在浏览器中运行
.htaccess
文件在某些web服务器上使用。档案在哪里?为什么它们是你的角度项目的一部分?在构建过程中是否需要它们?@joel.htaccess文件由Angular CLI自动生成。我不需要被忽略的文件成为项目的一部分,我只需要它被重写规则忽略。啊,我不知道angular CLI可以生成一个.htaccess文件。怎样?但是您部署在使用.htaccess文件的Web服务器上,对吗?难道你不能从你的应用程序发行版中排除ignorefile.json吗?如果这是一个实际存在的文件,那么用
检查请求文件名的两个重写条件-f
-d
标志应防止以下规则执行任何重写。不确定实际问题应该在哪里?忽略文件是什么意思?如果您键入
http://example.com/ignorefile.json
,您希望得到什么结果?404?主页?文件本身?