Question

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/

Was it helpful?

Solution

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]

OTHER TIPS

You could put this in your head:

<base href="http://www.example.com/subdirectory/">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top