If you want to keep the 'href="/css/layout.css"' part, then yes, you can set up a rewrite rule. You just have to be careful not to redirect anything starting with /testsite (or you'll end up with a loop).
Like (in root .htaccess):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/testsite/.*$
RewriteRule ^(.*)$ /testsite/$1 [QSA,L]
There, you will be able to access your layout.css file either from:
Sure, all relative URIs (as yours) are relative to the base URI which by default is the URI of the document:
http://localhost/testsite/index.php
You only need to tell which part to add:
<link rel="stylesheet" type="text/css" href="./css/layout.css" />
^
` see this dot
Example:
Base URI :: http://localhost/testsite/index.php
Relative URI :: ./css/layout.css
Result :: http://localhost/testsite/css/layout.css
If you need to have this more modular, there are multiple ways to do that. One is to set the base URI explicitly inside the HTML document (see <base>).
Another one is to resolve links relatively on the server-side based on the Request URI and your site's root-path. See this answer for an example.
发布评论
评论(2)
如果您想保留 'href="/css/layout.css"' 部分,那么可以,您可以设置重写规则。您只需小心不要重定向以 /testsite 开头的任何内容(否则您将最终陷入循环)。
就像(在 root .htaccess 中):
在那里,您将能够从以下位置访问您的 layout.css 文件:
http://localhost/ testsite/css/layout.css
或
http://localhost/css/layout.css
If you want to keep the 'href="/css/layout.css"' part, then yes, you can set up a rewrite rule. You just have to be careful not to redirect anything starting with /testsite (or you'll end up with a loop).
Like (in root .htaccess):
There, you will be able to access your layout.css file either from:
http://localhost/testsite/css/layout.css
or
http://localhost/css/layout.css
当然,所有相对 URI(如您的)都是相对于基本 URI 的,默认情况下,基本 URI 是文档的 URI:
您只需要告诉要添加哪一部分:
示例:
如果您需要更加模块化,有多种方法这样做。一种是在 HTML 文档中显式设置基本 URI(请参阅
)。另一种方法是根据请求 URI 和站点的根路径在服务器端相对解析链接。有关示例,请参阅此答案。
Sure, all relative URIs (as yours) are relative to the base URI which by default is the URI of the document:
You only need to tell which part to add:
Example:
If you need to have this more modular, there are multiple ways to do that. One is to set the base URI explicitly inside the HTML document (see
<base>
).Another one is to resolve links relatively on the server-side based on the Request URI and your site's root-path. See this answer for an example.