Webmaster Tip: Use absolute paths!
Posted by Jon Lee in Web Development, tags: fix, mod_rewrite, tips, web developmentWhen creating a website, here is a tip to save you a ton of headache later. Use absolute paths!! Using an absolute path means putting backslash at the beginning of the reference) For example, if you are embedding an image or a link, use:
<img xsrc=”/images/picture.png” />
<a xhref=”/contact.html”>Contact us</a>
instead of
<img xsrc=”images/picture.png” />
<a xhref=”contact.html”>Contact us</a>
even if the page in question is located in the root directory. Why? Because later on, the page might have to be moved to another folder and all the paths will need to be updated. (yes Dreamweaver does this for you). But more importantly, you may decide to use a little mod_rewrite to spice up your URLs. What was previously
http://www.jonlee.ca/index.php?page=1234
may now become
http://www.jonlee.ca/1234/
When the client loads your page, it is blind to the fact that the page is being “mod_rewritten” and will attempt to find images at
http://www.jonlee.ca/1234/images/picture.png
unless you have an absolute path specified!! So save yourself a lot of trouble now and use them whenever you can! And if you haven’t been using it… there are some clever quick fixes, such as Find and Replace the text src=”images/ with src=”/images/ or replace url(“images/ with url(“/images/
Not a foolproof or very elegant solution, but depending on your implementation, it could do the trick.
Correction: Thanks to those in the users in the comments and also those who e-mailed me to let me know that a true absolute path includes http://www.jonlee.ca/… etc. By adding a slash in front of a link, you are simply navigating to the root of where the files are stored on your web server, not necessarily the root of the file system itself. Anyway, the idea remains the same even thought he terminology is wrong
Popularity: 1% [?]
Entries (RSS)