Welcome to WebmasterWorld Guest from

Forum Moderators: phranque

Message Too Old, No Replies

Custom 404 page for sub directories

How does a custom 404 message work for subdirectories

11:34 am on Nov 20, 2013 (gmt 0)

New User

joined:Nov 20, 2013
posts: 1
votes: 0

Howdy experts

I want to set up a custom 404 page in the root of the site and for selected sub directories as the example below illustartes

www.webshop.com - "general 404" page
www.webshop.com/shoes - "shoes 404" page
www.webshop.com/pants - "pants 404" page

First of all is this possible and how would it work? Will the subdirectory 404 for shoes be shown only if a user is clicking a broken link that used to belong in the shoes subdirectory like www.webshop.com/shoes/brokenlink.htm? Or could you also set it up so a user would get the shoes 404 page if he follows a broken link from a page in the /shoes subdirectory

Do you have any other thoughts about custom 404 for sub directories pro/con.

I hope the above makes sense.

12:55 pm on Nov 20, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
votes: 244

Are you on Apache? Either in .htaccess or in <Directory> sections of the config file, put an

ErrorDocument blahblah

line in each directory where you want it to be different. You can't have <Directory> sections in htaccess, so you would have to make additional htaccess files for any subdirectories with rules of their own. Anything in a deeper directory overrides anything further up the line. (Except for mod_rewrite, almost everything in Apache behaves this way.)

The choice of ErrorDocument is determined by the URL of the not-found file. If you want something more complicated-- like a document determined by the referer rather than the URL itself-- you would have to create a dynamic error document. Make a single php-or-equivalent page in a single location, and code it to deal with any variables you want it to deal with. You would also need to do something fancier if your directories don't physically exist but are created by rewriting from, say, one central index.php page.

But for normal purposes, a static document is all you need. Note that the physical location of the error document itself doesn't matter. You can have a /shoes-errors.html and a /pants-errors.html and a /codpiece-errors.html all living side by side in the same directory. (Mine's called /boilerplate/, because I have no imagination.)

Reminder! An error document is a special kind of rewrite; the user's browser "thinks" it's at the originally requested URL. So if the error document uses any external files such as css, make sure all links are absolute.