I just read your post on Exploring Path Info. I think it is pretty much exactly what I was looking for BUT I am not much of a programmer so I can't implement it. I have to change the domain of a site and need to 301 redirect all the URLs to the new domain... It is not a matter of just forwarding the old domain to the new - I need to forward literally every old URL to every new URL. This all has to do with maintain the search engine rankings in Google. All totaled there are well over 300,000 URLs that need to be redirected.
NOTE: Made one change to the code where query string was returning NULL.
There are a lot of variables that go into this. Things like URL Rewriting and 404 error handling can make this stuff much more complicated and need to be addressed individually. For this demo, I am going to assume that we are dealing with straight-up URLs and that no crazy URL magic is going on.
That being said, all we have to do for this is get the currently requested page URL and then swap out the old domain value for the new domain value using simple string manipulation. There are a few different ways to get the URL parts such as the ColdFusion CGI scope; however, I have been told that those are unreliable going from server to server, so for ease of use, I am going to use the URL String Buffer returned by the underlying ColdFusion Request object. Actually, this is doubly-nice since the StringBuffer already takes care of things like protocol, credentials, and port, so we don't even have to worry about HOW to string together all the URL parts.
Now, we just need a centralized place in the site so that all incoming ColdFusion URLs are redirected. NOTE that I say ColdFusion requests since Image and other File requests are not handled through the ColdFusion application server. I only know this aspect (the ColdFusion aspect). The Application.cfc or Application.cfm file serves as the easiest place to put this code:
<!--- Get the request object for the current page. ---> <cfset objRequest = GetPageContext().GetRequest() /> <!--- Get the requested URL string buffer (this will NOT contain the query string). ---> <cfset objURLBuffer = objRequest.GetRequestURL() /> <!--- Get the query string for the current request. ---> <cfset strQueryString = objRequest.GetQueryString() /> <!--- Append the query string to the URL buffer if one exists for the current request. If no query string was present, the GetQueryString() method call will return NULL. ---> <cfif StructKeyExists( VARIABLES, "strQueryString" )> <cfset objURLBuffer.Append( "?" & strQueryString ) /> </cfif> <!--- ASSERT: At this point, our URL string buffer holds the full URL of the page that is currently being requested. Now that we have that, we need to forward users to another domain. ---> <!--- Convert the URL buffer to a string. ---> <cfset strURL = objURLBuffer.ToString() /> <!--- Instead of just blindly replacing domain values, make sure that it exists (if for some reason, something goes wrong, you might create an infinite loop of forwarding (if the domain never changes). CAUTION: I am using a regular expression here in case you want to hanlde thinks like www or what not. If you don't care about that, just use FindNoCase() and remove the \b constructs. ---> <cfif REFindNoCase( "\bOLD_DOMAIN\b", strURL )> <!--- We found the OLD domain in the current URL. Now, let's replace it with the NEW domain. CAUTION: I am using regular expression here in case you want to handle things differently. If you don't care about RegEx, just use a ReplaceNoCase() instead. ---> <cfset strNewURL = strURL.ReplaceFirst( "\bswoop\b", "NEW_DOMAIN" ) /> <!--- Return the permanent redirect to new URL. ---> <cfheader statuscode="301" statustext="Moved Permanently" /> <!--- Return new URL. ---> <cfheader name="Location" value="#strNewURL#" /> <cfelse> <!--- Something went wrong with our code and we are not finding the old domain. Return a 500 error. ---> <cfheader statuscode="500" statustext="Internal Server Error" /> <!--- Clear the content buffer and return HTML. ---> <cfcontent type="text/html" reset="true" /> <!--- Even though something went wrong, provide the user with a fail-safe so that they can at least get to the new domain. ---> <p> There was an error with the current page request. Please access our new site <a href="http://NEW_DOMAIN_HOME_PAGE">here</a>. </p> </cfif> <!--- Abort out of page. ---> <cfabort />
You will notice that in my redirect code, I am not just blindly swapping domain values and returning the headers; first I am checking to see if the domain is found. Once you get your code up and running, this shouldn't be a problem, but, at least for testing, this will help prevent and infinite redirect loop from occurring.
Also, just incase something does go wrong, I am returning a 500 error and giving the user a last-chance link to get to the new site. Hope this helps a bit.
Want to use code from this post? Check out the license.