How to rewrite absolute asset paths in a webserver subdirectory

I use absolute paths for links to resources like css, images and javascript. So, in <head> of index.html , I have something like this:

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

Assuming my project directory is as follows:

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

This works fine on my local web server, where I install the document root in this directory using the <virtualhost> directive. But when I put this in a webserver subdirectory (e.g. http://www.example.com/subdirectory/ ), it no longer finds assets when accessing http://www.example.com/subdirectory/index.html .

How can I solve this without using relative paths in index.html ? Can this be done using a .htaccess file in a subdirectory? If so, how?

Edit

There are other folders at the root level of the web server that should not be affected by the redirection that occurs for /subdirectory/

+4
source share
2 answers

If everything is below subdirectory , you can use 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] 
+3
source

You can put this on your head:

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

All Articles