我使用绝对路径来引用像 css、图像和 javascript 文件这样的 Assets 。所以在 <head>index.html 我有这样的东西:

<link href="/assets/css/style.css" rel="stylesheet">
假设我的项目目录如下所示:
- 索引.html
- Assets /
-- css/
--- style.css

这在我的本地网络服务器上运行良好,我使用 <virtualhost> 指令将文档根目录设置为该目录。但是当我把它放在网络服务器的子目录(例如 http://www.example.com/subdirectory/ )时,它在访问 http://www.example.com/subdirectory/index.html 时不再找到 Assets 。
如何在不必在 index.html 中使用相对路径的情况下解决这个问题?可以通过子目录中的 .htaccess 文件实现吗?如果是,如何?
编辑
网络服务器的根级别上还有其他文件夹,不应受到 /subdirectory/ 发生的任何重定向的影响

最佳答案

如果一切都低于 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]

关于html - 如何重写 webserver 子目录上的绝对 Assets 路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16286598/

10-12 12:34
查看更多