大网数据广告活动
网站运营与故障问答

How to redirect all pages of site A, including those with query strings, to homepage of site B

· 52CE 我爱测

I want to redirect all pages of SiteA to homepage of SiteB . I used this code in SiteA's .htaccess file: <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^(.*)$ https://SiteB.tld/ [R=301,L] </IfModule> The code works perfect…

问题原文

原帖链接 · Pasakgroup · CC BY-SA 4.0

I want to redirect all pages of SiteA to homepage of SiteB.

I used this code in SiteA's .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.*)$ https://SiteB.tld/ [R=301,L]
</IfModule>


The code works perfectly, but not for URLs with Question Mark, such as

https://SiteA.tld/?action=dayview&calendar=1&year=2000&month=01&day=01


This pages will get 404 Not Found and wouldn't redirect to SiteB's hompage.

What changes should I make in the Redirection code?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

You should add the "query string discard" or QSD flag to your rewrite rule:

RewriteRule ^(.*)$ https://SiteB.tld/ [R=301,QSD,L]

From the documentation:

When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded.

This flag is available in version 2.4.0 and later.

Using [QSD] and [QSA] together will result in [QSD] taking precedence.

If the target URI has a query string, the default behavior will be observed - that is, the original query string will be discarded and replaced with the query string in the RewriteRule target URI.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。