.htaccess 如何在htaccess中重写url以隐藏子文件夹?

.htaccess 如何在htaccess中重写url以隐藏子文件夹?,.htaccess,url-rewriting,.htaccess,Url Rewriting,我的网站位于我的公共html文件夹的子文件夹(gng2)中。public_html/.htaccess文件中的以下代码工作正常,因为键入my domain可以从子目录正确加载网站: ## redirect to subfolder containing the app. RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR] RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteCond %{REQUEST_U

我的网站位于我的公共html文件夹的子文件夹(gng2)中。public_html/.htaccess文件中的以下代码工作正常,因为键入my domain可以从子目录正确加载网站:

## redirect to subfolder containing the  app.
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !gng2/
RewriteRule (.*) /gng2/$1 [L]
我的问题是这也行。我想将这些URL重写/重新路由到,这样“gng2”子文件夹就不会出现在Google Analytics结果中

我应该如何修改上述代码来实现这一点

谢谢,
W.

请将您的htaccess文件保存在gng2文件夹中,并尝试在您的htaccess规则文件中执行以下规则。请确保这些规则位于文件顶部(用于将http/HTTPS应用于URL,以防您有更多规则),并且请在测试URL之前清除浏览器缓存

RewriteEngine ON
## redirect to subfolder containing the  app.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
##Place your RewriteRule here.....

您可以在
gng2/.htaccess
中使用此重定向规则从所有URL中删除
/gng2/

重新编写引擎打开
RewriteCond%{THE_REQUEST}\s/+gng2/(\s*)[NC]
重写规则^/%1[L,R=301,NE]
#其他规则显示在此行下方

您的意思是我应该将.htaccess文件从public\u html移动到public\u html/gng2文件夹吗?或者我应该在gng2子文件夹中创建第二个.htaccess文件,其中只包含这些条目吗?为什么我不能将所有条目都放在public_html/.htaccess文件中?谢谢谢谢我修改了我的public_html/.htaccess,如下所示,但即使在清除缓存后,它也不会重写url。有什么想法吗重定向到包含gng2应用程序的子文件夹。RewriteCond%{THE_REQUEST}\s/+gng2/(\s*)[NC]RewriteRule^/%1[L,R=301,NE]RewriteCond%{HTTP_HOST}^example.com$[NC,或]RewriteCond%{HTTP_HOST}^www.example.com$RewriteCond%{REQUEST_URI}!gng2/RewriteRule(.*)/gng2/$1[L]请看下面我要重写的url示例:我使用工具:使用此url测试我的htaccess内容,它说您建议的条件不符合…
madewithlove.be
对于测试重写规则来说是非常无用的。它甚至不支持请求。最好的测试地点是在本地apache上。我刚刚检查了您是否使用
server:nginx
而不是apache。nginx不支持所有mod_重写规则。更好地了解nginx文档您能解释一下为什么我需要将.htaccess文件移动到public_html/gng2文件夹吗?我能否不简单地将这些条目添加到public_html/.htaccess文件中现有条目的顶部?谢谢