4

I'm using absolute paths to reference assets like css, images and javascript files. So in the <head> of index.html I have something like this:

<link href="/assets/css/style.css" rel="stylesheet">

Assuming that my project directory looks like this:

- index.html
- assets/
-- css/
--- style.css

This works fine on my local webserver where I set the document root to this directory using a <virtualhost> directive. But when I put this on a webserver's subdirectory (e.g http://www.example.com/subdirectory/), it doesn't find the assets anymore when accessing http://www.example.com/subdirectory/index.html.

How can I solve that without having to use relative paths in index.html? Can it be achieved with an .htaccess file in the subdirectory? If yes, how?

Edit

There are other folders on the webserver's root level, that shouldn't be affected by any redirect that occurs for /subdirectory/

Community
  • 1
  • 1
mediafreakch
  • 1,336
  • 1
  • 12
  • 19
  • /assets/css/style.css is a relative path, so I'm not sure what you mean. – dave Apr 29 '13 at 19:28
  • @dave that's not true, it's absolute because it starts with a slash. – Nelson Apr 29 '13 at 19:29
  • The solution is to use relative paths, in spite you don't like it. – Nelson Apr 29 '13 at 19:31
  • It is true, it is relative to the document root. An absolute path would include example.com. – dave Apr 29 '13 at 19:32
  • You are right, actually it's a relative path I'm using (both /assets/ and assets/) are considered to be relative paths. But elsewhere, /assets/ is mentioned as best practice: http://stackoverflow.com/questions/2005079/absolute-vs-relative-urls – mediafreakch Apr 29 '13 at 20:17

2 Answers2

3

If everything is below subdirectory, you can use a simple rewrite

RewriteEngine on
# don't rewrite, if it is already rewritten
RewriteCond %{REQUEST_URI} !^/subdirectory
# only rewrite requests for files below /assets
RewriteCond %{REQUEST_URI} ^/assets/
RewriteRule ^.*$ /subdirectory/$0 [L]
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • 1
    this doesn't work. Browser still requesting http://www.example.com/assets/css/style.css – mediafreakch Apr 30 '13 at 06:00
  • @mediafreakch It works in my test environment. Do you have the .htaccess in the root directory? – Olaf Dietsche Apr 30 '13 at 08:13
  • Ah, no I had placed it in /subdirectory . If I place it in the root directory it basically works. But now I understand what you meant by "everything is below subdirectory". It's not the case in my setup. There are many other directories that shouldn't be affected. How do I need to modify your solution to not affect the others? – mediafreakch Apr 30 '13 at 11:42
  • @mediafreakch You can restrict the rule to `assets` for example. See updated answer. – Olaf Dietsche Apr 30 '13 at 14:22
  • Uhm..I hope the OP doesn't have an `assets/` directory within root ;) – Erenor Paz Mar 07 '15 at 15:50
0

You could put this in your head:

<base href="http://www.example.com/subdirectory/">
dave
  • 62,300
  • 5
  • 72
  • 93