菜鸟的 mod 重写和相对 url 问题
我一定做了一些非常错误的事情,因为无论我如何尝试或谷歌它,我都找不到答案:(
所以,我想要一个像 http://localhost/BLUEPRINT/list.php?lid =857
我可以在.htaccess文件中编写重写规则,并且可以读取lid变量。问题是 list.php 中的所有路径都是相对的。 Css、图像、javascript 等因此,当 SEO 友好的 url 加载时,所有这些项目都会在 BLUEPRINT/list/857/... 中查找,
例如: 在请求 seo 友好的 url 时实际上是这样的:
那么我能做什么?
我可能可以尝试将页面中的所有路径转换为根相对路径(例如“/BLUEPRINT/images/logo.png”)而不是相对路径。但页面中有几十个,即使我这样做,它们也不会在实际服务器上工作,因为它应该是“/images”而不是“/BLUEPRINT/images”。所以我不能只是将文件上传到实际服务器。
我有什么选择?所有这些出色的脚本(如 wordpress、joomla 等)如何处理这个问题?我到底做错了什么?这让我发疯!
I must be doing something terribly wrong because no matter how i try or google it, i can't find an answer :(
So, i want to have a url like http://localhost/BLUEPRINT/list/857
to actually load a perfectly working url like this: http://localhost/BLUEPRINT/list.php?lid=857
I can write the rewrite rule in the .htaccess file and i can read the lid variable. The problem is that all the paths in list.php are relative. Css, images, javascript e.t.c. So when the SEO-friendly url loads all those items are looked inside BLUEPRINT/list/857/...
So for example this: <img src="images/logo.png" />
is actually something like this when requesting the seo-friendly url: <img src="list/857/images/logo.png" />
So what can i do?
I could probably try to convert all paths in the page(s) to root relative (ex. "/BLUEPRINT/images/logo.png") instead of relative. But there are dozens in the page and even if i do, they will not work on the actual server because there it sould be "/images" instead of "/BLUEPRINT/images". So i could not just upload my files to the actual server.
What are my options? How do all these wonderful scripts out there like wordpress, joomla, e.t.c. handle this problem? What the hell am i doing wrong? It drives me crazy!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的新网址没有相同的基本路径前缀。这意味着相对 URL 路径的解析方式不同。引用
images/logo.png
现在解析为/BLUEPRINT/list/images/logo.png
而不是/BLUEPRINT/images/logo.png< /代码>。
要解决此问题,请在引用中使用绝对 URL 路径,例如:
或者,如果您不想将基本 URL 路径
/BLUEPRINT/
添加到所有引用元素,请使用 < a href="http://www.w3.org/TR/html4/struct/links.html#edef-BASE" rel="nofollow noreferrer">base
:现在是相对的URL 路径是从此基本 URL 而不是当前 URL 解析的。但请注意,更改基本 URL 会影响所有相对 URL。
Your new URLs don’t have the same base path prefix. That means relative URL paths are resolved differently. The reference
images/logo.png
is now resolved to/BLUEPRINT/list/images/logo.png
instead of to/BLUEPRINT/images/logo.png
.To fix this either use absolute URL paths in your references like:
Or, if you don’t want to add the base URL path
/BLUEPRINT/
to all of your referencing elements, set the base URL withbase
:Now relative URL paths are resolved from this base URL instead of the current URL. But note that changing the base URL affect all relative URLs.
您可以编写一个条件,允许对 CSS、JS 和其他路径的请求通过,并忽略您对 list.php 的重写规则。这是一个例子。
这是 Apache 提供的一个很棒的教程,其中包含大量示例: http://httpd .apache.org/docs/2.2/misc/rewriteguide.html
祝你好运!
编辑: Apache 的另一个很好的链接是 mod_rewrite 文档。 http://httpd.apache.org/docs/2.2/mod/mod_rewrite。 html 对“passthrough”进行文本搜索。
RewriteRule 中需要注意的另一部分是破折号“-”。这意味着“不应进行替换”。
最后,我将 RewriteEngine 和 RewriteBase 规则添加到示例中。这应该使它更加完整。
You can write a condition which allows the requests to CSS, JS and other paths to passthrough and ignore your rewrite rules for list.php. Here is an example.
Here is a great tutorial from Apache with a ton of examples: http://httpd.apache.org/docs/2.2/misc/rewriteguide.html
Good Luck!
EDIT: Another good link from Apache is the mod_rewrite docs. http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html Do a text search for 'passthrough'.
Another part of the RewriteRule to note is the dash '-'. It means that "no substitution should be performed."
Finally, I've added the RewriteEngine and RewriteBase rules to the example. This should make it more complete.
您是否尝试过应用“RewriteBase”规则?
http://httpd.apache.org/docs/2.0/mod/ mod_rewrite.html#rewritebase
Have you tried applying the "RewriteBase"-Rule?
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritebase