将所有子页面重定向到主页,使用htaccess

发布于 2025-01-30 19:07:11 字数 222 浏览 2 评论 0原文

我想将所有子页面重定向到主页。我尝试使用代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2025-02-06 19:07:11

您需要使用查询字符串重定向任何非空的URL路径或主页(空URL路径)。您还应 删除查询字符串作为重定向的一部分(您的规则当前保留了从初始请求中保留查询字符串)。

例如,请尝试以下内容:

# Redirect everything to the homepage (same domain)
RewriteCond %{REQUEST_URI} ^/. [OR]
RewriteCond %{QUERY_STRING} .
RewriteRule ^ http://www.example.com/ [QSD,R=301,L]

上述状态...对于所有URL路径,其中URL路径由至少一个字符(初始斜线之后)组成或包含一个查询字符串,然后重定向到根。

QSD标志从请求中丢弃原始查询字符串。

规则上的NC标志是多余的,因为无论如何您都不匹配特定的字母。

旁边:,我会质疑这样做这样的动机。搜索引擎(Google)将大量重定向到主页,将其视为软404,因此执行此操作没有SEO好处,如果用户遵循以前存在的链接,通常会使他们感到困惑。在这种情况下,有意义的404响应通常是首选选项。


更新:

如果我想使用此代码来重定向到其他域,我应该更改或添加到重定向主页?

假设另一个域也指向另一个服务器,那么您只需要在上述规则上删除两个条件即可重定向所有内容并删除查询字符串。

例如:

# Redirect everything to the homepage on an external domain
RewriteRule ^ http://www.example.com/ [QSD,R=301,L]

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:

# Redirect everything to the homepage (same domain)
RewriteCond %{REQUEST_URI} ^/. [OR]
RewriteCond %{QUERY_STRING} .
RewriteRule ^ http://www.example.com/ [QSD,R=301,L]

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:

If i would like to use this code also to redirect to other domain what should i change or add to redirect also main page?

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:

# Redirect everything to the homepage on an external domain
RewriteRule ^ http://www.example.com/ [QSD,R=301,L]
浊酒尽余欢 2025-02-06 19:07:11

尝试一下,它在我的系统中起作用。

RewriteRule ^(.*)$ http://www.example.com/ [L,R=301]

Try this, it works in my system.

RewriteRule ^(.*)$ http://www.example.com/ [L,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文