如何将网站从
htdocs
重定向到子目录/htdocs/web
并且不在URL中显示/web
?
htdocs/.htaccess
中当前的工作解决方案:
RewriteEngine On
RewriteRule ^(.*)$ /web/$1 [L]
它仅适用于主页 - 它不显示
web
,但在访问任何子页面时,例如About
页面,网址显示为example.com/web/about
,但应该是example.com/about
。
注意这是一个带数据库的动态 CMS PHP 网站,没有静态页面。共享托管也非常有限,无法编辑任何 Apache 配置等,只能编辑 htdocs 中的内容。
更新2
此方法最初会在从主页访问子页面时删除
/web/
,但当例如从子页面内的同一菜单重新访问同一页面或其他子页面。
RewriteRule ^(.*)$ web/$1 [L,NC]
我的理解是,您希望在内部将请求从根目录重定向到子目录
(htdocs/web)
,而不暴露 URL 中的 /web/
部分。
当前解决方案面临的问题是,当您导航到子页面时,它将 /web/
添加回 URL
。
使用以下修改后的.htaccess rules
:
RewriteEngine On
# Rewrite requests to /web/ for all URLs except the /web/ itself
RewriteCond %{REQUEST_URI} !^/web/
RewriteRule ^(.*)$ /web/$1 [L]
# Rewrite requests to remove /web/ from the URL for all URLs within /web/
RewriteCond %{REQUEST_URI} ^/web/
RewriteRule ^web/(.*)$ $1 [L]
注: 确保将此
modified .htaccess
文件放置在您网站的根目录(htdocs)
下才能生效。
进行这些更改后,您的 URL 结构应按预期运行,并且 /web/
不会出现在 URL 中。
#Apache-Age #htaccess