htaccess 隐藏目录名称不起作用

htaccess hide directory name doesn't work


我的服务器上有多个站点和域。
我不知道我做错了什么,但是当我切换我的站点目录时,地址路径中显示了主目录路径。

/.htaccess

RewriteEngine On
RewriteBase /
Options -Indexes

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule !^example_home_dir/ /example_home_dir%{REQUEST_URI} [L,NC]

当我在主页上时 - 一切正常(example.com 正在显示 example_home_dir 的内容)。
当我手动输入带子目录的域时,everythink 也工作正常(示例。com/subdir 显示子目录内容)。
当我单击重定向到 "example.com/subdir" 的锚点时,服务器将我重定向到 "example.com/example_main_dir/subdir" 并显示子目录内容。

我尝试了很多解决方案,但没有任何效果。
出于好奇,我会说重定向在我的 symfony 项目中工作正常。

我不知道该怎么办:( 非常感谢您的帮助。

您可以使用重定向规则从 URL 中删除 example_home_dir:

DirectorySlash Off
Options -Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+example_home_dir/(\S*) [NC]
RewriteRule ^ /%1 [L,NE,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule !^example_home_dir/ example_home_dir%{REQUEST_URI} [L,NC]

当你有一个指向目录的 URL 时,还要确保你总是有一个尾部斜杠。所以使用 example.com/subdir/ 而不是 example.com/subdir.

确保使用新浏览器来测试此更改。