How to permanently redirect domain in ASP.NET without using code-behind, and track redirected traffic as a Campaign in Google Analytics

No specific Bitcoin Bounty has been announced by author. Still, anyone could send Bitcoin Tips to those who provide a good answer.

We have a domain that receives some type-in traffic. We want to redirect this traffic to the main domain and also be able to track that traffic as a Campaign in Google Analytics on the main site.

The webserver is IIS8 and it already has an "empty website" bound to this domain to be redirected.

Basically, what is the easiest way to permanently redirect domain to some external domain in ASP.NET without compiling anything and without using any code-behind? How to track that redirected traffic in Google Analytics?



3 answers

Internet users could send Bitcoin Tips to you if they like your answer!

1. Prepare tracking URL for GA:

Since you want to track that traffic as a separate GA Campaign, you would first need to prepare a tracking URL with some params that GA uses for campaign tracking. You would then be redirecting to that tracking url. You could either write it manually or use URL builder from Google. You should end up with tracking URL looking similar to this example:

2. Perform ASP.NET permanent redirect without code behind:

Now we need to redirect to that tracking URL without using code-behind.

The redirect is in fact an HTTP response with HTTP status code either 301 for permanent redirects or 302-307 for temporary and other types of redirects, and new redirect destination url being included as an HTTP header with the name "Location".

Here is how to do a 301 permanent redirect without using any code-behind. We would use VB.NET syntax or you could change it to C#. First, you need to create a default.aspx file at your root folder in IIS8, then copy-paste the code below into default.aspx file:

<%@ Page language="vb" AutoEventWireup="true"%>

<script runat="server">

Sub Page_Load

   Response.Status = "301 Moved Permanently" 

   Response.AddHeader( "Location" ,"")

End Sub


Your redirect should now work, but here is one more quick thing to check:

3. Check meta canonical on the redirected page to reduce chances of double-indexing by google

I am not sure how smart (or not) is google these days to not to double index landing pages where the only difference is google's own campaign tracking parameters, but still... it is better that the redirected page has <meta canonical ...> tag that lists its original url without any GA tracking parameters, to avoid any duplicate content issues, as explained by Google here. So in our case the landing page should have the following canonical link in its <head> :

<link rel="canonical" href="" />

Now finally you are done and you could monitor the newly redirected traffic in Google Analytics Campaigns.

HTH! .


You can add new Headers in the IIS Panel(menu item "Response"):

HTTP/1.1 301 Moved Permanently


But, I didn't test this method :)


Alternatively, instead of WRITING code in default.aspx file, you could

  • Open IIS 8 Internet Services Manager
  • Locate your site to be redirected
  • Locate "HTTP Redirect" icon
  • Double-click on that icon to open "HTTP Redirect" screen
  • Enter the Redirect URL, Response Code (301), check some checkboxes, click "Apply" link

enter image description here

enter image description here


Too many commands? Learning new syntax? is a free tool to save your favorite scripts and commands, then quickly find and copy-paste your commands with just few clicks.

Boost your productivity with!

Post Answer