首页 > 解决方案 > 如何使用 htaccess 隐藏 URL 上的父文件夹?

问题描述

我已经对这个问题进行了广泛的搜索,但无法找到解决方案。我在本地工作,所以我有这个网址:

http://localhost/IASON/iasongitlab/static_website/html/solutions.html

我设法隐藏了文件扩展名,所以现在我的 URL 是这样的:

http://localhost/IASON/iasongitlab/static_website/html/solutions

但现在我需要从我的 URL 中隐藏 /html/ 文件夹,使它看起来像这样:

http://localhost/IASON/iasongitlab/static_website/solutions

这只是一个例子。我有多个视图,所以我需要在 htaccess 中编写代码来隐藏每个视图上的 /html/ 文件夹。

这是我现在的 htaccess 文件:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L] 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

我尝试了多种建议但均未成功,希望对解决此问题有任何见解。

标签: .htaccessurlmod-rewritedirectoryhide

解决方案


使用您显示的示例,您能否尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。

RewriteEngine ON
RewriteRule ^(IASON/iasongitlab/static_website)/(solutions)/?$ $1/html/$2.html [NC,L]


或者,如果您不想硬编码路径详细信息并solutions最终在 uri 中查找,那么您可以尝试跟随。确保一次只放置以下或以上规则。

RewriteEngine ON
RewriteRule ^(.*)/(solutions)/?$ $1/html/$2.html [NC,L]

推荐阅读