3

I want 301 redirect from all pages of SiteA.example to Home Page of SiteB.example using .htaccess rewrite rule, Since I am not PHP guy I am not sure if below rule is correct

RewriteRule ^(.*)$ https://www.SiteB.example/?utm_campaign=domainnames&utm_medium=domain&utm_source=%{HTTP_HOST}" [L,R=301,QSA] [R=301,QSD,L]

I am facing problem since I am not able to track 301 redirects on SiteB.example as Google Analytics doesn't track it as this information is not passed on when 301 redirect is taking place.

So rewrite rule was suggested by someone on Stack Overflow but I want to get it right. Can you confirm that the above rewrite rule is fine?

8
  • Do SiteA and SiteB point to different servers? What exactly do you want to redirect from/to? You say you want to redirect to the "home page", but what is utm_campaign=domainnames&utm_medium=domain&utm_source=%{HTTP_HOST} for? In the directive you posted you are explicitly preserving the query string from the original request - do you need this? "Since i am not PHP guy" - this has nothing to do with PHP.
    – MrWhite
    Commented Jul 26, 2022 at 15:25
  • @MrWhite, I want all pages of SiteA.com to be redirected to home page of siteB.com in a way so that i can redirect it with UTM so that i can track referral/ traffic source on SiteB.com on GA
    – Learning
    Commented Jul 27, 2022 at 7:34
  • So, this is really a GA problem? The RewriteRule directive itself is "OK" (except that you've duplicated the flags argument - which I assume this is just a typo - but this won't actually cause an "error"). But you didn't answer my first question... "Do SiteA and SiteB point to different servers?" If not and SiteA/B is just an alias then the above will naturally result in a redirect loop.
    – MrWhite
    Commented Jul 27, 2022 at 8:45
  • 1
    @MrWhite, Do SiteA and SiteB point to different servers? Yes both are on different server... I did resolve it by using following as part of query string https://SiteB.com/?utm_campaign=SiteARedirect&utm_medium=301Redirect&utm_source=SiteAWebsite now i am able to see traffic under Traffic Sources in Real Time and under Acquisition-->Campaigns-->All Campaigns before this it was not possible as 301 doesnt often pass this information in header so GA is not able to register it. So this is a way around to track source traffic in this scenario
    – Learning
    Commented Jul 28, 2022 at 4:22
  • 1
    This is the code in .httaccess RewriteRule ^(.*)$ https://SiteB.com/?utm_campaign=SiteARedirect&utm_medium=301Redirect&utm_source=SiteAWebsite [R=301,L]
    – Learning
    Commented Jul 28, 2022 at 4:34

1 Answer 1

1

The following solution works for me. This code was added to the .htaccess file and it redirected all the pages in Site A to the Home Page of Site B.

RewriteEngine On
RewriteRule ^(.*)$ https://SiteB.com/utm_campaign=SiteARedirect&utm_medium=301Redirect&utm_source=SiteAWebsite [R=301,L]

Your Answer

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

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