【问题标题】:Redirect all requests to specific directory without changing URL .htaccess (Deploying Angular project)将所有请求重定向到特定目录而不更改 URL .htaccess(部署 Angular 项目)
【发布时间】:2021-04-10 04:21:57
【问题描述】:

我有一个 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/.../ ..../.....

感谢您的帮助!

【问题讨论】:

标签: angular .htaccess redirect mod-rewrite directory


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    相关资源
    最近更新 更多