在子文件夹中使用角度重定向

时间:2019-05-13 08:40:09

标签: angular .htaccess

我用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]

2 个答案:

答案 0 :(得分:2)

请在.htacccess文件上使用此代码,希望对您有用。

谢谢

<IfModule mod_rewrite.c>
    RewriteEngine on

    # Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Rewrite everything else to index.html
    # to allow html5 state links
    RewriteRule ^ index.html [L]
</IfModule>

答案 1 :(得分:0)

在使用此.htaccess的子文件夹中的斜角的路径上,我取得了成功(请记住将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/">