Reputation: 3792
I have many domains (www.vf1.com, www.vf2.com, www.vf3.com etc) which all point to my main domain (www.vf.com). How do I do a 301 redirect from these other domains to my main domain?
So if someone hits www.vf1.com/news/1234 they should get redirected to www.vf.com/news/1234
I also have domains such as images.vf.com, css.vf.com and js.vf.com that I don't need redirecting
I'm using ColdFusion 8 on IIS (but I don't have access to IIS at the moment)
I tried the below on a URL such as http://www.festivalreviews.co.uk/latest/news/14500 but it gets redirected to http://www.virtualfestivals.com/index.cfm
<cfif cgi.http_host IS 'www.festivalreviews.co.uk'>
<cfset jjURL = 'http://www.virtualfestivals.com' & CGI.PATH_INFO>
<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="#jjURL#">
</cfif>
Thanks
Upvotes: 0
Views: 387
Reputation: 2287
Since ColdFusion 8, I use <cflocation> (because CF8 added the statusCode attribute) for these kinds of redirects.
<cfif CGI.SERVER_NAME EQ 'www.festivalreviews.co.uk'>
<cfset jjURL = 'http://www.virtualfestivals.com' & CGI.SCRIPT_NAME>
<cfif CGI.QUERY_STRING NEQ ''>
<cfset jjURL = jjURL & '?' & CGI.QUERY_STRING>
</cfif>
<cflocation url="#jjURL#" addtoken="no" statuscode="301">
</cfif>
<cflocation> documentation: https://wikidocs.adobe.com/wiki/display/coldfusionen/cflocation
Upvotes: 0
Reputation: 13548
I agree with Adam, if possible, this would probably be better handled using your web server. Having said that, I have had to do similar things in the past with ColdFusion. When I need to rebuild URLs I typically use different CGI variables than what you tried. See if this works for you.
<cfif CGI.SERVER_NAME IS 'www.festivalreviews.co.uk'>
<cfset jjURL = 'http://www.virtualfestivals.com' & CGI.SCRIPT_NAME>
<cfif CGI.QUERY_STRING NEQ ''>
<cfset jjURL = jjURL & '?' & CGI.QUERY_STRING>
</cfif>
<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="#jjURL#">
</cfif>
CGI.SERVER_NAME
- Server's hostname, DNS alias, or IP address
CGI.SCRIPT_NAME
- Virtual path to the script that is executing
CGI.QUERY_STRING
- Query information that follows the ? in the URL that referenced this script
You can read more about the CGI variables in the documentation here.
Upvotes: 1