htaccess, 404 page and relative paths

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
After following some tutorials I created a special 404 error page in the
same layout as the rest of my site and specified some corresponding
lines in the .htaccess file in the root directory of the site.
It works like a dream, so I was feeling very professional :),

The directory structure of the site is like:
root, containing
  - index.htm
  - .htaccess
  - stylesheet
  subdirectory containing some standard images
    sub subdirectory1
  subdirectory: errors
    contains 404.htm

All files in all subdirectories refer to the stylesheet in the root and
some standard header images in the 'images' directory, using the
appropriate relative paths like ../stylesheet etc. That also applies to
the 404page.

The question:
Url: domain/wrongname gives the correct 404page
Url: domain/subdirectory/wrongname gives the correct 404page
Url: domain/subdirectory/subsubdirectory/wrongname gives the 404-page
with broken links to the headerimages and without the style.

I fixed it by using the absolute addresses in the 404page, so the
problem is over. But for the sake of the learning-curve: what could be
the explanation of the variable "path-level" of the called 404page?


Re: htaccess, 404 page and relative paths

Quoted text here. Click to load it

The 404 page is being served instead of the requested page - look at
the location bar of your browser and you'll see
http://domain/subdirectory/subsubdirectory/wrongname not

So the browser will resolve all relative URLs relative to the
incorrect URL.

404.htm may contain ../stylesheet. Where does that resolve to for the
three examples?

domain/wrongname --> domain/../stylesheet which is error corrected to

domain/subdirectory/wrongname --> domain/subdirectory/../stylesheet
which is domain/stylesheet

domain/subdirectory/subsubdirectory/wrongname -->
domain/subdirectory/subsubdirectory/../stylesheet which is

You can use site root relative URLs instead of absolute URLs. So
href="/stylesheet" in your case. This will always be resolved relative
to the root of the site and hence will always work regardless of where
the 404 document is returned.


"My theories appal you, my heresies outrage you,
 I never answer letters and you don't like my tie."  - The Doctor

Re: htaccess, 404 page and relative paths

Steve Pugh wrote:
Quoted text here. Click to load it
That's quick, to the point and clear.


Site Timeline