How to get godaddy to work with custom 404 on multiple asp.net websites
Posted on Jul 27, 2009 by
Paul WhiteFirst of all we love
godaddy because you can run many
websites from a single
asp.net hosting account. So your $6.99 / month can power 25
websites all that get their own
MySQL DB. After that you might have to get creative in your database handling. If you try to use every
SEO trick in the book the chances are that you are using some sort of URL rewriting to get this done. Some use HTTP handlers, I prefer to use custom 404.aspx pages to decode the requests and get them transferred to the right page.
When you only run a single website with it being based at the root of your
hosting account this would be easy. All you have to do is login to your
Godaddy account manager and change your 404 redirects to be whatever page you want. But when you have multiple
websites each in their own folder at the root, you don't get to declare a custom 404 for each folder. All the requests would go to whatever you has hosted at the root directory of your
hosting account.
So After some trial and error I figured out how to make it all work.
Step 1. First no website should be running on your root. So goto your domain manager and change your primary domain to be some made up domain. (IE mykickasswebsites.com ) What this will do is create the HostHeaders in IIS to for this domain. You don't have to own this domain, just make it what ever you want. They might give you some warning message just ignore that. It won't matter since you aren't
hosting anything at the root.
Step 2. Create a directory for each website you intend to run. The best way to do this is to keep the directories relavent to your domains. Example if you own the domain. "www.onecoolwebsite.com" Make the directory "onecoolwebsite".
Step 3. The next step is to make these directories application roots. to do this goto the IIS management section. You will need to create each directory and make sure you set it with anonymouse access and Set Application Root.
Step 4. In your Root directory you will need a master file that will server as a router to the right application's 404.aspx file.
Mine looked like this. I called it 404root.aspx
void Page_Load(Object Src, EventArgs E){
string strPageName = Request.ServerVariables["QUERY_STRING"].ToString();
strPageName=strPageName.ToLower();
string [] names=strPageName.Split('/');
string pagename=names[names.Length-1];
string domain = Request.ServerVariables["HTTP_HOST"].ToString();
domain=domain.ToLower();
domain=domain.Replace("www.","");
domain=domain.Replace(".com","");
string jumpto=domain+"/404.aspx?key="+pagename;
Server.Transfer(jumpto,true);
}
Next you have to setup
Godaddy to use this for handling 404 errors. do do this goto Settings and 404 error behavior. Select "Use custom page", and put 404root.aspx in the box then click continue and submit. It might take a few minutes for this to go through.
What the 404root.aspx does is takes all requests that were not found on all of your
websites and routes the to the 404.aspx page on each given website's folder.
Then on each website's folder you need a 404.aspx page that will handle the individual website's requests. But there is a catch. On your website's 404.aspx page, when you do your final Server.Transfer to the final destination page, you must include the directory its in.
Here is the code in my website's 404.aspx
void Page_Load(Object Src, EventArgs E){
Response.Write("success");
string strPageName = Request.ServerVariables["QUERY_STRING"].ToString();
string calltype="";
// Trim Off .htm From Location Name
if ((strPageName[strPageName.Length-1]=='m') && (strPageName[strPageName.Length-2]=='t') && (strPageName[strPageName.Length-3]=='h') && (strPageName[strPageName.Length-4]=='.')){
strPageName = strPageName.Substring(0,strPageName.Length-4);
calltype=".htm";
}
else{
Server.Transfer("404error.aspx");
}
string key="";
string key2="";
string destination="";
// get destimation file
int end=strPageName.Length;
int num=strPageName.Length-1;
while(strPageName[num]!='_' && num>5){
num--;
}
num++;
destination=strPageName.Substring(num,end-num);
// get object key
num--;
end=num;
num--;
while(strPageName[num]!='_' && num>5){
num--;
}
num++;
key=strPageName.Substring(num,end-num);
// get object key
num--;
end=num;
num--;
while(strPageName[num]!='_' && num>5){
num--;
}
num++;
key2=strPageName.Substring(num,end-num);
string jumpto="";
switch(destination){
case "person":
jumpto="person.aspx?key="+key;
break;
case "events":
jumpto="events.aspx?key="+key;
break;
default:
jumpto="default.aspx";
break;
}
Server.Transfer("MyWebsiteDirectory/"+jumpto);
}
I hope that helps everyone who is using
asp.net on
godaddy and trying to get multiple website to handle custom 404 Server.Transfers.
This is awesome but will it work on Linux hosting via Go Daddy? It's a shame it has to be this complex, they should have it set up to customize 404s for each domain in the control panel.