我使用的是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]
我如何在编译应用程序时忽略一个特定的文件?
INFO
Angular不会生成&处理服务器端重定向。
为了处理Angular中从一个视图到下一个视图的导航,你使用Angular路由器。路由器通过将浏览器的URL解释为改变视图的指令来实现导航。
当路由器导航到一个新的组件视图时,它会用该视图的URL更新浏览器的位置和历史记录。由于这是一个严格的本地URL,浏览器不会将这个URL发送到服务器,也不会重新加载页面。
如果你使用的是(Angular 9默认的)现代浏览器的PathLocationStrategy,它们支持history.pushState,这种技术可以在不触发服务器页面请求的情况下改变浏览器的位置和历史记录。路由器可以组成一个 "自然 "的URL,这个URL与其他需要页面加载的URL是无法区分的。
在这种情况下,服务器需要将所有流量重定向到index.html,这样Angular就可以接管处理UI上的路径。为此,你必须在你的生产服务器上附加一个.htaccess文件,以使路由工作。
答案
如果你试图将ignorefile.json与你的angular应用一起提供,这是很有可能的。你应该更新你的angular.json文件。
"assets": [
...
{
"glob": "ignorefile.json",
"input": "src/",
"output": "/"
},
...
]
foo.com将加载Angular应用,但foo.comignorefile.json将显示JSON数据。
使用与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]