首页 > 解决方案 > 在子文件夹中使用 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]

标签: angular.htaccess

解决方案


我在使用这个 .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/">

推荐阅读