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

How to redirect all pages of site A, including those with query strings, to homepage of site B - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, 301-redirect, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


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?


Solution :

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.



Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect