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/

4

2 回答 2

3

如果一切都在下面subdirectory,您可以使用简单的重写

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]
于 2013-04-29T20:52:31.310 回答
0

你可以把它放在你的脑海中:

<base href="http://www.example.com/subdirectory/">
于 2013-04-29T19:32:12.133 回答