angular - 在子文件夹中使用 Angular 进行重定向
问题描述
我用 angular 创建了一个迷你应用程序,我想把它放在我的网站上的一个子文件夹中。一切正常,除了当我刷新页面时,它发现我出现 404 错误。如何确保重定向到我的 Angular 应用程序的 index.html(在子文件夹中?)
我尝试使用此代码:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html/projet/angular/appBookList [NC,L]
解决方案
我在使用这个 .htaccess 的子文件夹中的 angular 路径中取得了成功(请记住SUBFOLDER
用您的特定文件夹替换)。
# Disables content negotiation (choosing the right file in ambiguous cases)
Options -Multiviews
# Enable rewriting rules
RewriteEngine on
# CASE 1: FILES
# =============
# The next three lines define 3 options (ORed, that is, only true)
# Is a regular file, OR
RewriteCond %{REQUEST_FILENAME} -s [OR]
# Is a symbolic link, OR
RewriteCond %{REQUEST_FILENAME} -l [OR]
# Is a directory
RewriteCond %{REQUEST_FILENAME} -d
# If any of the above are true, rewrite:
# ^ = start of line; .* = match any character, 0 or more times; $ = end of line
# - = no substitution
# NC = no case matching
# L = do not match any more rules
RewriteRule ^.*$ - [NC,L]
# CASE 2: Subpaths (SPA)
# ======================
# ^ = start of line; () = matching group; .* = match any character, 0 or more times
# Rewrite everything to /index.html
# NC = no case matching
# L = do not match any more rules
RewriteRule ^(.*) /SUBFOLDER/index.html [NC,L]
此外,在 index.html 中指明子文件夹的基础也很重要:
<base href="/SUBFOLDER/">
推荐阅读
- python - AttributeError: 'list' 对象在将 DF 附加到列表时没有属性'to_excel',并将这些 DF 的保存与 openpyxl 连接到聚合文件
- python - 如何使用主窗口自动调整 Qwidget 的大小?
- python - 如何将 C 二进制缓冲区转换为 Python 字符串中的十六进制表示?
- docker - Docker 容器不运行 crontab
- python - 传入装饰函数的参数
- css - 如何对齐包裹在两行上的左标签
- d3.js - 将旧geo适配到D3 v5,如何表达一个Promise的队列?
- javascript - 如何使锚标签和按钮标签具有相同的样式?
- node.js - 使用不同卡为现有客户创建费用
- python - PANDAS ValueError:无法从重复轴重新索引 - 尝试用零回填缺失的日期