Aug 14, 2011
tom

Virtual Directory Path in HTML Files

Question

I tried to create a development server by copying a website from one host to a second different host, but the servers seem to handle virtual paths differently.

On the main server this works:

<link href="/styles/styles.css" rel="stylesheet" type="text/css">

On the dev server the same path would need to be written as (without the first slash):

<link href="styles/styles.css" rel="stylesheet" type="text/css">

How can I make the dev server work with the main servers files without rewritting the paths? Is this something I can do from .htaccess with mod rewrite?

Answer

First, some background on how paths work. Absolute paths are based on the server root. Relative paths are based on the URL (not the file path) of the current page. However, this behavior is NOT handled by the server–its handled by the client, and this behavior is universal (Yes, even IE6… I know, surprise!). The only thing that would cause these two paths to NOT be the same would be if they’re not relative to the root of the web site.

For example, if we’re trying to access the URL you mentioned above (/styles/styles.css), here’s how they would behave on the following two examples:

“Main Server” web site hosted at http://mainwebsite/index.html

styles/styles.css  => http://mainwebsite/styles/styles.css
/styles/styles.css => http://mainwebsite/styles/styles.css

“Dev Server” web site hosted at http://devsite/some_sub_dir/index.html

styles/styles.css  => http://devsite/some_sub_dir/styles/styles.css
/styles/styles.css => http://devsite/styles/styles.css

If you clarify your answer (these servers are clearly not hosting the same data in the same way), I could provide a more accurate description. However, if I had to guess, I’d say you’re probably running into the issue I described.

Good luck!

Andrew

Related posts:

  1. Permissions issue with virtual directory to UNC path
  2. How can I prevent people from looking at a listing of files in parent directory if I haven’t uploaded index.html?
  3. IIS 6 – how to create virtual directory like /my/custom/path
  4. Parse PHP in HTML files – files download instead of parse
  5. Apache RewriteRule to treat images, css and js files as normal

Leave a comment