我使用绝对路径来引用资源,例如 css、图像和 javascript 文件。所以在 <head>
的 index.html
我有这样的东西:
<link href="/assets/css/style.css" rel="stylesheet">
假设我的项目目录如下所示:
- index.html - assets/ -- css/ --- style.css
这在我的本地网络服务器上运行良好,我使用 <virtualhost>
将文档根目录设置为该目录指示。但是当我把它放在网络服务器的子目录中(例如 http://www.example.com/subdirectory/
)时,它在访问 http://www.example.com/subdirectory/index.html
时不再找到 Assets 了。 .
如何在不使用 index.html
中的相对路径的情况下解决该问题? ?可以通过子目录中的 .htaccess 文件来实现吗?如果是,如何?
编辑
Web 服务器的根级别上还有其他文件夹,这些文件夹不应受到为 /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 - 如何重写网络服务器子目录上的绝对 Assets 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286598/