首页 > 解决方案 > 使用 .htaccess 在 URL 中隐藏文件夹

问题描述

如何将我的 URL 从显示:更改www.example.com/p/tools为仅www.example.com/tools

我尝试过使用 .htaccess ,但不幸的是无法使其工作。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
</IfModule>

标签: .htaccessurl-rewriting

解决方案


根据您显示的示例,您能否尝试以下操作。

请确保在测试您的 URL 之前清除您的浏览器缓存

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteRule ^p/(.*)/?$ $1 [NC,R=301,L]

RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
</IfModule>

推荐阅读