2

If user types in this url

https://DNSName1/Work/Service?serviceId=11#tab_Foo

It should take them to

https://DNSName2/Work/Service?serviceId=11#tab_Foo

My website is deployed on IIS (8.5) Windows server 2012 machine. Also it is a MVC application

Looked into IIS http redirect and it seems like it doesn't preserve the query string.

StackOverflowVeryHelpful
  • 2,347
  • 8
  • 34
  • 46
  • Did you at least tried to search anything??? Just two hints: Request.Url and Response.Redirect. – Gusman Aug 26 '15 at 17:29
  • quick search on stackoverflow could give you something like this http://stackoverflow.com/questions/6985068/how-can-i-redirect-to-a-url – NKD Aug 26 '15 at 17:32
  • @Gusman. Did search on both google and stackoverflow. http://stackoverflow.com/questions/6527156/redirect-to-a-directory-preserving-the-query-string. Had no correct marked answer here. http://www.wikihow.com/Redirect-a-URL – StackOverflowVeryHelpful Aug 26 '15 at 17:38
  • 1
    Request.Url.ToString() will give you the url including the query string, then Response.Redirect(Request.Url.ToString().Replace("oldDns", "newDns")) will redirect including the query string variables... – Gusman Aug 26 '15 at 17:41
  • something like: `Response.Redirect(Request.Url.AbsoluteUri.Replace("DNSName1", "DNSName2");` – DDan Aug 26 '15 at 17:43

1 Answers1

2

Try like this:

Response.Redirect(Request.Url.AbsoluteUri.Replace("DNSName1", "DNSName2"));
  • Response.Redirect Redirects to new url.

  • Request.Url.AbsoluteUri Gets you the current URL including query string

  • Replace("DNSName1", "DNSName2") sets your new DNS name to the new one.

DDan
  • 8,068
  • 5
  • 33
  • 52