Custom 404 Page on Windows Shared Hosting

A.viary.com 404 Page by @MSG on FlickrI recently had to setup a new WordPress blog for work and when it came time to setting up the 404 page, I had the hardest time finding answers for what I was trying to do. So, I thought I would post my solution so it’s out there and ideally save someone hours of wasted time.

The Situation

I am hosting my companies new blog with DomainsMadeEasy (identical to GoDaddy — I have not investigated if the two companies are related, but at first site, you’d assume they are since all of the tools used for domain registration, management, hosting, etc. are identical) on a Windows server (need it for some ASP.NET development we are doing).

In any case, I am hosting www.mydomain.com and the WordPress blog is using the subdomain, blog.mydomain.com. After everything was setup and ready to go, I wanted to ensure the 404 page setup within WordPress was working properly. So, I typed in a bogus address, http://blog.mydomain.com/dsifsdf, to see what I got. Sure enough, I received the ugly default 404 page. In other words, WordPress wasn’t picking up the 404.

Since DomainsMadeEasy does not delegate the “Error Handler” setting on Windows servers, I was unable to work with the web.config file and as I mentioned earlier, moving to Linux to use the .htaccess file was not an option. After hours of searching, a few calls to support and being told it is “impossible,” I finally took a break. When I returned to the issue, the answer dawned on me…

The Solution

404 Error Pages by Dave Delaney on FlickrIn short, it kind of IS impossible to use custom 404 pages for each subdomain and therefore need to trick the server. You can only have one 404 page under the main domain. This also means, if you have an account that allows hosting multiple domains, whatever domain you setup as the primary will be forced to host the 404 page. Example: mydomain.com is the primary, but I’m also hosting thisdomain.com under the same account. If a 404 is served for thisdomain.com, the 404 page for mydomain.com will actually be served…not cool.

Well, the answer ended up being quite simple, though, not perfect). With a little PHP coding, you can create a 404 page that actually redirects to the 404 page of the domain you wish.

First, you need to setup your custom 404 pages for each domain and subdomain you want a custom 404 page for. If you are using WordPress, you can setup a new page from your admin tools to serve this purpose. Note: with the below solution, WordPress will not know this is a 404 error coming through.

Next, you need to create a 404.php page to use under the primary domain. This page will filter the incoming request and forward it on to the appropriate custom 404 page you just setup. My PHP code within this page, looks something like…

$ref = $_SERVER['SERVER_NAME'];
if(strpos($ref, "otherdomain.com"))
{
header( 'Location: http://otherdomain.com/errorpage' ) ;
}
elseif(strpos($ref, "blog.mydomain.com"))
{
header( 'Location: http://blog.mydomain.com/errorpage' ) ;
}
else
{
header( 'Location: http://www.mydomain.com/error/' ) ;
}

After your 404.php page is set, upload it to your host. I put mine right in the root of mydomain.com. Once your 404.php page is uploaded and in place, login to your hosting account. Once logged in, navigate to the 404 page settings. For GoDaddy and DomainsMadeEasy, this is under Settings, 404 Error Behavior. From there, select the “Use custom page” option. Set the option to point to your 404.php file: http://www.mydomain.com/404.php (note, the http://www.mydomain.com/ part of the entry can not be altered. You should only be typing 404.php into the text box, unless you placed the 404.php file into a subfolder). Once you think the setting looks correct, click on “Continue” and save your changes.

After 10 – 30 minutes, your settings should update and you should now have a custom 404 page for every domain you have hosted under your account.

That’s all there is to it. I hope this helps someone out there.

Till next time…

Related Posts with Thumbnails
Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars
2 votes, average: 4.50 out of 5
Loading...
Categorized: How Tos & Know Hows