mediafreakch
mediafreakch

Reputation: 1415

How to rewrite absolute assets paths on webserver subdirectory

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/

Upvotes: 5

Views: 3905

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74078

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]

Upvotes: 3

dave
dave

Reputation: 64667

You could put this in your head:

<base href="http://www.example.com/subdirectory/">

Upvotes: 0

Related Questions