将所有请求重定向到特定目录而不更改 URL .htaccess(部署 Angular 项目)

Redirect all requests to specific directory without changing URL .htaccess (Deploying Angular project)

我有一个 Angular 项目,我想将它部署到一个特定的文件夹(不是根目录)上,就像这样: http:///localhost/folder/index.html 所以我需要在不更改 URL.

的情况下重定向此路径上的所有请求

当 index.html 位于根目录时,以下工作正常(因此,http://localhost/index.html):

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

所以我尝试像这样更改此代码以用于我的项目:

RewriteRule ^ /folder/index.html

我可以很好地处理这个请求:http://localhost/folder/abcd 但不能处理 http://localhost/folder/abcd/efgh、http://localhost/folder/abcd/efgh/.../。 .../.....

感谢您的帮助!

能否将您的 .htaccess 文件放入名为 folder 的文件夹中?请确保在测试您的 URL 之前清除浏览器缓存。

如评论中所述:在您网页的 header 中添加此 <base href="/"> 以便您的相关链接可以从正确的位置加载

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]