htaccess, 404 page and relative paths

J

Jsp

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 :),
but...

The directory structure of the site is like:
root, containing
- index.htm
- .htaccess
- stylesheet
subdirectory containing some standard images
subdirectory1
sub subdirectory1
etc.
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?

Jsp
Site: http://www.lahaina.nl
 
S

Steve Pugh

Jsp said:
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.
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?

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
http://domain/errors/404.htm

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/stylesheet

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

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

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.

Steve
 
J

Jsp

Steve said:
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
http://domain/errors/404.htm

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/stylesheet

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

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

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.

Steve
That's quick, to the point and clear.
Thanks!

Jsp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top