Redirect using Coldfusion or Redirect using IIS
I have a client who has url say www.xyz.com. They want a new url www.abc.com so that when user enters www.xyz.com, the user should see www.abc.com in his address bar of browser. Basically an alias/redirection with address bar showing the new url. I could have done redirection by dns names/url redirection at website level but problem is I have a subfolder application i.e. www.xyz.com/subapp.
Now if I do dns/url redirection, and user tries to access subfolder application with this url, www.xyz.com/subapp/home.cfm, it takes the user automatically to www.abc.com/home.cfm i.e. the main application without the subfolder application, and I dont want that. It should take user to www.abc.com/subfolder/home.cfm.
So to avoid this redirection at the website level, I added this code in Application.cfm
Code:
<!-- Relocate if not a secure connection -->
<cfif cgi.SERVER_PORT is "80" and CGI.SERVER_PORT_SECURE is "0">
<!---replace xyz with abc --->
<cfset x = "https://#Replace(cgi.HTTP_HOST,"xyz","abc")##cgi.PATH_INFO#">
<cfif cgi.query_string is "">
<cfset y = "">
<cfelse>
<cfset y = "?#cgi.query_string#">
</cfif>
<cflocation url="#x##y#"><cfabort>
<cfelseif cgi.HTTP_HOST contains "xyz">
<!---replace xyz with abc--->
<cfset x = "https://#Replace(cgi.HTTP_HOST,"xyz","abc")##cgi.PATH_INFO#">
<cfif cgi.query_string is "">
<cfset y = "">
<cfelse>
<cfset y = "?#cgi.query_string#">
</cfif>
<cflocation url="#x##y#"><cfabort>
</cfif>
But I found a better solution after going through IIS documentation. See below.
What I did is, went to IIS 6, website, properties, Home Directory and under Resource, I selected A Redirection to a URL and in Redirect to field, I entered my new url appended with $S and $Q. For example, www.abc.com$S$Q.
$S as per IIS docs, Passes the matched suffix of the requested URL. The matched suffix is the portion of the original URL that remains after the redirected URL is substituted.
$Q Passes both the question mark (?) and the parameters from the original URL.
so now, www.xyz.com/subapp/home.cfm takes me to www.abc.com/subapp/home.cfm and lets say if i have
www.xyz.com/subapp/home.cfm?var1=yes then its www.abc.com/subapp/home.cfm?var1=yes . The caveat here to note is that, if I did not append $Q, the var1 was not being passed to the new url. So adding $Q fixed that issue.
Last thing, under The client will be sent to option, I selected The exact URL entered above.
I hope this information is helpful.
So far this is working great. I will post back if I hear of any issues from the client.
Now if I do dns/url redirection, and user tries to access subfolder application with this url, www.xyz.com/subapp/home.cfm, it takes the user automatically to www.abc.com/home.cfm i.e. the main application without the subfolder application, and I dont want that. It should take user to www.abc.com/subfolder/home.cfm.
So to avoid this redirection at the website level, I added this code in Application.cfm
Code:
<!-- Relocate if not a secure connection -->
<cfif cgi.SERVER_PORT is "80" and CGI.SERVER_PORT_SECURE is "0">
<!---replace xyz with abc --->
<cfset x = "https://#Replace(cgi.HTTP_HOST,"xyz","abc")##cgi.PATH_INFO#">
<cfif cgi.query_string is "">
<cfset y = "">
<cfelse>
<cfset y = "?#cgi.query_string#">
</cfif>
<cflocation url="#x##y#"><cfabort>
<cfelseif cgi.HTTP_HOST contains "xyz">
<!---replace xyz with abc--->
<cfset x = "https://#Replace(cgi.HTTP_HOST,"xyz","abc")##cgi.PATH_INFO#">
<cfif cgi.query_string is "">
<cfset y = "">
<cfelse>
<cfset y = "?#cgi.query_string#">
</cfif>
<cflocation url="#x##y#"><cfabort>
</cfif>
But I found a better solution after going through IIS documentation. See below.
What I did is, went to IIS 6, website, properties, Home Directory and under Resource, I selected A Redirection to a URL and in Redirect to field, I entered my new url appended with $S and $Q. For example, www.abc.com$S$Q.
$S as per IIS docs, Passes the matched suffix of the requested URL. The matched suffix is the portion of the original URL that remains after the redirected URL is substituted.
$Q Passes both the question mark (?) and the parameters from the original URL.
so now, www.xyz.com/subapp/home.cfm takes me to www.abc.com/subapp/home.cfm and lets say if i have
www.xyz.com/subapp/home.cfm?var1=yes then its www.abc.com/subapp/home.cfm?var1=yes . The caveat here to note is that, if I did not append $Q, the var1 was not being passed to the new url. So adding $Q fixed that issue.
Last thing, under The client will be sent to option, I selected The exact URL entered above.
I hope this information is helpful.
So far this is working great. I will post back if I hear of any issues from the client.
Comments
Thanks,
Krishan Pal