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:
Launch code in new window » Download code as text file »
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.
Download Code Snippet ZIP File
Comments (6) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
GetPageContext() Is Template Specific, Not Request Specific
Creating Excel Files With ColdFusion, XML, And POI
Alternative and in my opinion preferred way is to use mod_rewrite if using Apache. Couple fo lines in .htaccess will take care of whole thing (including images and other files)
RewriteEngine On
RewriteRule ^(.*)$ http://www.new.com/$1 [R=301,L]
IIS has something similar, I don't know exact solution for it.
Posted by Tero Pikala on Aug 22, 2007 at 7:59 AM
@Tero,
That looks pretty nice. Unfortunately, I don't have access to any rewriting filters where I host / test, so I can't go this route. In your example, maybe I am just not seeing it, but where are you matching the old domain name? It appears as if the current domain name isn't even part of the URL that is exposed to MOD Rewrite? Also, will $1 start with a forward slash?
Posted by Ben Nadel on Aug 22, 2007 at 9:02 AM
That would go to .htaccess in your webroot; only requests for that domain will be served there and thus matching is done earlier by Apache virtual hosts setup.
If your host allows you to use .htaccess and they have mod_rewrite enabled this would work for you - I would think that's quite common but it has been ages since I last used anything but dedicated servers one way or another.
I think you are right about forward slash, I just googled for example and copied what they had (http://www.tamingthebeast.net/articles3/changing-domain-names.htm)
Posted by Tero Pikala on Aug 22, 2007 at 9:50 AM
Ben, I'd be surprised if your current host does not allow .htaccess. I think that almost all hosts allow it. I know plenty of people who use it to help set up they blogs address string in wordpress. It has become a quite common feature with most webhosting.
Posted by Dave Butler on Aug 23, 2007 at 1:13 PM
@Dave,
To be honest, I know nothing about htaccess files. In general, other than a 404 handler, I don't know much about anything that requires additional CFAdmin or IIS setup. I don't have access to that stuff, so I find ways of doing stuff without it.
Posted by Ben Nadel on Aug 23, 2007 at 1:21 PM
You're using a very commendable process to go about this. Scanning for the site first, etc. I can imagine the nightmare the original asker of the question must have gone through trying to do everything one by one like that. Surely there must be an easier way! You do have to assume, though, that no URL magic is going on - so, you still need to probably at least check one by one.
Posted by vacuum cleaners on Dec 11, 2007 at 8:13 AM