我正在编写一个带有 SEO 友好链接的网站,即将页面标题或其他描述性文本放在链接中,并用斜杠分隔。例如:h*tp://www.domain.com/section/page-title-bla-bla-bla/。
我使用 mod_rewrite 将请求重定向到主脚本,但 script、img 和 link 标记中的链接未正确解析。例如:假设您正在访问上述链接,则标记会请求 URL h*tp://www.domain.com/section/page-title-bla-bla-bla/js/file 处的文件。 js,但该文件实际上是 http://www.domain.com/js/file.js
我不想在所有 HTML 文件 URL 中使用变量或常量。
我正在尝试将客户端请求重定向到目录或另一个服务器。可以区分页面的第一个请求和之后的请求吗? Apache 或 PHP 可以使用 mod_rewrite 吗?
我希望我解释得很好:)
提前致谢。
I'm programming a website with SEO friendly links, ie, put the page title or other descriptive text in the link, separated by slashes. For example: h*tp://www.domain.com/section/page-title-bla-bla-bla/.
I redirect the request to the main script with mod_rewrite, but links in script, img and link tags are not resolved correctly. For example: assuming you are visiting the above link, the tag request the file at the URL h*tp://www.domain.com/section/page-title-bla-bla-bla/js/file.js, but the file is actually http://www.domain.com/js/file.js
I do not want to use a variable or constant in all HTML file URLs.
I'm trying to redirect client requests to a directory or to another of the server. It is possible to distinguish the first request for a page, which comes after? It is possible to do with mod_rewrite for Apache, or PHP?
I hope I explained well:)
Thanks in advance.
发布评论
评论(2)
使用重写规则来解决相对路径的问题是不明智的,并且有很多缺点。
首先,它使维护变得更加困难,因为系统中有数百个不同的链接。
其次,更严重的是,您破坏了可缓存性。从这里请求的资源:
将被视为不同的资源
并被加载两次,导致请求数量增长数十倍。
该怎么办?
解决问题的根本原因:使用绝对路径
或常量,或者如果所有其他方法都失败了
标记。Using rewrite rules to fix the problem of relative paths is unwise and has numberous downsides.
Firstly, it makes things more difficult to maintain because there are hundreds of different links in your system.
Secondly and more seriously, you destroy cacheability. A resource requested from here:
will be regarded as a different resource from
and loaded two times, causing the number of requests to grow dozenfold.
What to do?
Fix the root cause of the problem instead: Use absolute paths
or a constant, or if all else fails the
<base>
tag.这是解析相对URI的问题。从您的描述来看,您似乎使用相对 URI 路径引用其他资源:在
/section/page-title-bla-bla-bla
中,像js/file.js 这样的 URI 引用
或./js/file.js
将解析为/section/page-title-bla-bla-bla/js/file.js
。要始终独立于实际基本 URI 路径引用
/js/file.js
,请使用绝对路径/js/file.js
。另一种解决方案是使用 / rel="nofollow noreferrer">BASE
元素(但请注意,这将影响所有相对 URI)。This is an issue of resolving relative URIs. Judging by your description, it seems that you reference the other resources using relative URI paths: In
/section/page-title-bla-bla-bla
a URI reference likejs/file.js
or./js/file.js
would be resolved to/section/page-title-bla-bla-bla/js/file.js
.To always reference
/js/file.js
independet from the actual base URI path, use the absolute path/js/file.js
. Another solution would be to set the base URI explicitly to/
using theBASE
element (but note that this will affect all relative URIs).