将所有子页面重定向到主页,使用htaccess
我想将所有子页面重定向到主页。我尝试使用代码:
RewriteEngine On
RewriteRule .+ http://www.example.com [r=301,nc,l]
所有子页面均重定向,除了包括问号的这些子页面,例如,http://www.example.com/?123
未重定向。如何修改我的代码也重定向这些URL?
I want to redirect all subpages to main page. I tried with code:
RewriteEngine On
RewriteRule .+ http://www.example.com [r=301,nc,l]
All subpages are redirected except these that include question mark, for example, http://www.example.com/?123
is not redirected. How to modify my code to redirect also those URLs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用查询字符串重定向任何非空的URL路径或主页(空URL路径)。您还应 删除查询字符串作为重定向的一部分(您的规则当前保留了从初始请求中保留查询字符串)。
例如,请尝试以下内容:
上述状态...对于所有URL路径,其中URL路径由至少一个字符(初始斜线之后)组成或包含一个查询字符串,然后重定向到根。
QSD
标志从请求中丢弃原始查询字符串。规则上的
NC
标志是多余的,因为无论如何您都不匹配特定的字母。旁边:,我会质疑这样做这样的动机。搜索引擎(Google)将大量重定向到主页,将其视为软404,因此执行此操作没有SEO好处,如果用户遵循以前存在的链接,通常会使他们感到困惑。在这种情况下,有意义的404响应通常是首选选项。
更新:
假设另一个域也指向另一个服务器,那么您只需要在上述规则上删除两个条件即可重定向所有内容并删除查询字符串。
例如:
You need to redirect any non-empty URL-path OR the homepage (empty URL-path) with a query string. You should also remove the query string as part of the redirect (your rule currently preserves the query string from the initial request).
For example, try the following instead:
The above states... for all URL-paths where the URL-path consists of at least one character (after the initial slash) OR contains a query string then redirect to the root.
The
QSD
flag discards the original query string from the request.The
NC
flag on the rule is superfluous, since you aren't matching specific letters anyway.Aside: However, I would question the motives for doing something like this. Search engines (Google) will see mass redirects to the homepage as soft-404s, so there is no SEO benefit in doing this and it can often be confusing for users if they are following a link that previously existed. A meaningful 404 response is usually the preferred option in this scenario.
UPDATE:
Assuming the other domain also points to a different server then you just need to remove the two conditions on the above rule to redirect everything and remove the query string.
For example:
尝试一下,它在我的系统中起作用。
Try this, it works in my system.