在语义 URL 中使用“/”作为分隔符是否始终需要绝对路径

2023-12-09

我们对 mod_rewrite 和语义 URL 很陌生,尽管研究了 Google 和 SO,但我们仍无法找到这个问题的明确答案。

当我们在 .htaccess 中使用正斜杠作为分隔符时,html 中所有的图像、css 和 js 引用都必须从相对路径重写为绝对路径。

例如:

RewriteRule ^([A-Za-z0-9-]+)/?$ inhalt.php?ebene1=$1 [NC,L] # process ebene1
RewriteRule ^([A-Za-z0-9-]+)\/([A-Za-z0-9-]+)/?$ inhalt.php?ebene1=$1&ebene2=$2 [NC,L] # process ebene1,2
RewriteRule ^([A-Za-z0-9-]+)\/([A-Za-z0-9-]+)\/([A-Za-z0-9-]+)/?$ inhalt.php?ebene1=$1&ebene2=$2&ebene3=$3 [NC,L] # process ebene1,2,3

意味着必须重写:

<link href="css/acssfile.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/ajsfile.js"></script>
<img src="imgs/animage file.gif" alt="" />

to:

<link href="http://www.oursite.com/css/acssfile.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://www.oursite.com/js/ajsfile.js"></script>
<img src="http://www.oursite.com/imgs/animage file.gif" alt="" />

这总是在语义 URL 中使用正斜杠作为分隔符的结果吗?还是有办法保留我们的相对路径?


浏览器正在解析相对于其所在 URL 的 CSS 包含内容。如果您的网址更改为inhalt.php to foobar/,那么浏览器现在处于不同的路径。当它开启时/inhalt.php,相对路径css/acssfile.css决心/css/acssfile.css。然而,当它开启时/foobar/,该相对路径解析为/foobar/css/acssfile.css.

最好使用绝对路径,但不要使用包括域的绝对 URL。因此,链接到您的资产文件:

<link href="/css/acssfile.css" rel="stylesheet" type="text/css" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在语义 URL 中使用“/”作为分隔符是否始终需要绝对路径 的相关文章