I’m in the process of making a website for myself but there is a niggling problem that if you type a URL and end with / or /(any characters) then you get a page without CSS.
For example:
http://www.acnorris.uk/contact works fine
/contact/ displays without CSS/JS/images
/contact/anythinghere displays without CSS/JS/images
The same also goes for my 404 page
http://www.acnorris.uk/pretendlink shows 404 fine
/pretendlink/ no CSS/JS/images
/pretendlink/somethingelse no CSS/JS/images
I have spent time looking at editing htaccess files (it’s hosted on a plesk server) but
I can’t work this out. Hopefully someone can offer a solution so even mistyped links
display with CSS.
2
Answers
Thanks for the advice everyone,
I figured out the problem was to do with me setting relative instead of absolute links which sorted out the problem. Rookie mistake :(
Adam
You’re probably missing the CSS because the browser is searching in different folders. For example, if a browser requests the
/contact
page, and finds<link href=contact.css>
, it will then request/contact.css
. If instead, the request is for/contact/
, it will request/contact/contact.css
.This behaviour may also be affected by the
<base>
HTML element.I don’t know what you’re using at the server end to translate URLs to filenames, but I’ll throw in one detailed example below.
General Structure
One common solution is to put all the files related to, for instance, your contact page in the same folder:
/contact/index.php
/contact/contact.css
/contact/contact.js
.From the index page, you can refer to the files using relative URLs (
src=contact.js
,href=contact.css
). Any site-wide files should be placed in the web root or a subfolder of the web root, and referred to using an absolute URL (/site.js
or/js/site.js
). You should probably choose better file names.Index file
In order for Apache to load the index file, you need to use mod_dir to specify a set of file names to search for. The server will attempt to load the files in the order specified:
Trailing slash
The
DirectoryIndex
will only take effect if the URL ends in a slash (/
), so mod_dir automatically redirects a URL that specifies a folder without a trailing slash.Note that the auto-redirect can be turned off in later versions of Apache: