如何通过.htaccess将/folder重写至子目录且保持地址栏URL不变?
解决.htaccess内部重写时地址栏跳转的问题
问题场景
要实现https://example.com/folder的内部重写,实际指向https://example.com/otherfolder/anotherfolder,但浏览器地址栏必须保持显示https://example.com/folder。之前尝试的规则执行后触发了地址栏跳转,无法隐藏真实路径。
之前使用的错误规则
RewriteEngine On RewriteRule ^folder/?$ /otherfolder/anotherfolder
问题根源
目标路径/otherfolder/anotherfolder缺少末尾斜杠,服务器访问无斜杠的目录时,会自动补全斜杠并触发重定向,导致地址栏URL发生变化。
修正后的正确规则
RewriteEngine On RewriteRule ^folder/?$ /otherfolder/anotherfolder/ [L]
- 给目标路径添加末尾
/:避免服务器自动补斜杠引发的重定向 - 增加
[L]标记:终止后续规则处理,确保当前规则优先生效
内容的提问来源于stack exchange,提问作者Eric Vinyard
相关产品推荐
相关产品推荐

