2

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?

4
  • 1
    Redirecting everything to a home page is not usually good for either users or for SEO. It is almost always better to redirect users to the same page on another site if it exists or to give them an error message explaining why it is gone. Jun 4, 2018 at 23:42
  • "This pages will get 404 Not Found and wouldn't redirect to SiteB's hompage." - Are you saying that SiteA triggers the 404 and the user is not redirected to SiteB at all? What version of Apache are you using?
    – DocRoot
    Jun 5, 2018 at 0:20
  • SiteA and its directories removed totally. SiteA's directories does not exist in SiteB. So, to use its DA and PA we decided to redirect its pages to an homepage of SiteB with related data. @stephen-ostermiller
    – Pasakgroup
    Jun 5, 2018 at 7:00
  • 1
    For pages with Question Mark, SiteA redirected to SiteB, but this pages get 404 Not Found in SiteB and wouldn't redirect to SiteB's homepage. The QSD flag has made everything OK. @DocRoot
    – Pasakgroup
    Jun 5, 2018 at 7:12

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.