如何在不改变相对路径的情况下使用 apache 的 mod_rewrite rewriterule
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/144651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use apache's mod_rewrite rewriterule without changing relative paths
提问by
I've the following rewrite rule in .htaccess:
我在 .htaccess 中有以下重写规则:
RewriteRule ^groups/([^/\.]+)/?$ groupdetail.php?gname= [L,NC]
This takes something like www.example.com/groups/groupname and calls www.example/groupdetail.php?gname=groupname. And it works just fine.
这需要类似于 www.example.com/groups/groupname 并调用 www.example/groupdetail.php?gname=groupname。它工作得很好。
But all the relative links on groupdetail.php use groups/ as the relative path, and I don't want them to. How do I avoid this?
但是 groupdetail.php 上的所有相对链接都使用 groups/ 作为相对路径,我不希望他们这样做。我如何避免这种情况?
For example, when a user clicks on a link <a href="link.php">on groupdetail.php?gname=groupname, he's taken to www.example/groups/link.php. I want to take the user to www.example.com/link.php.
例如,当用户点击<a href="link.php">groupdetail.php?gname=groupname上的链接时,他会被带到 www.example/groups/link.php。我想将用户带到 www.example.com/link.php。
Obviously, I want to URL to the user to look like "www.example.com/groups/groupname" so I don't want to use [R]/redirect.
显然,我想让用户的 URL 看起来像“www.example.com/groups/groupname”,所以我不想使用 [R]/redirect。
回答by
If like me you had hundreds of relative links in the page, insert a <base href="">in the <head>with an absolute path (could use relative too). You'll need to also make the path to .js files in the <head>absolute because IE and firefox deal with the base href differently. I agree it is an annoying issue.
如果像我一样在页面中有数百个相对链接,请<base href="">在<head>带有绝对路径的 中插入一个(也可以使用相对)。您还需要将 .js 文件的路径设为<head>绝对路径,因为 IE 和 firefox 处理 base href 的方式不同。我同意这是一个烦人的问题。
回答by
Relative links are resolved by the browser, not the server, so there is nothing you can do with mod_rewrite.
相对链接是由浏览器解析的,而不是服务器,所以你不能用 mod_rewrite 做任何事情。
Either use relative links the go up the hierarchy (../link.php) or use absolute links.
要么使用相对链接向上层次结构 ( ../link.php) 要么使用绝对链接。
回答by Robin
If you do not want to have absolute links or use <base>because you are going to move the page around, you can have the base be generated by php, as following:
如果你不想有绝对链接或者<base>因为你要移动页面而使用,你可以让基由php生成,如下所示:
echo '<base href="http://'.$_SERVER['SERVER_NAME'].str_replace("index.php","",$_SERVER['PHP_SELF']).'" />';
回答by Razvan
You can use the BASE tag, if you don't want to use absolute paths:
如果不想使用绝对路径,可以使用 BASE 标记:
回答by Kevin Hakanson
If you change the rewite rule to do a force redirect(add the [R] option), then the browser will be using the /groupdetail.php URL and the relative links will work fine. However, that adds one redirect and makes the URLs less pretty.
如果您更改 rewite 规则以执行强制重定向(添加 [R] 选项),则浏览器将使用 /groupdetail.php URL 并且相关链接将正常工作。然而,这增加了一个重定向并使 URL 变得不那么漂亮。
RewriteRule ^groups/([^/.]+)/?$ groupdetail.php?gname= [L,NC,R]
回答by Andrew Swift
Hop's answer is correct. The browser sees www.example.com/groups/groupnameas the address, so considers that /groupsis the current directory. So, any links like <a href=link.php>are assumed to be in the /groupsfolder.
霍普的回答是正确的。浏览器将其www.example.com/groups/groupname视为地址,因此认为这/groups是当前目录。因此,<a href=link.php>假设任何链接都在/groups文件夹中。
When the user moves his mouse over the link, he'll see www.example.com/groups/link.phpas the link address.
当用户将鼠标移到链接上时,他会看到www.example.com/groups/link.php链接地址。
The solution is to use absolute links -- just add a slash before the href:
解决方案是使用绝对链接——只需在 href 前添加一个斜杠:
<a href=/link.php>
<a href=/link.php>
The user will then see www.example.com/link.phpas the url.
然后用户将看到www.example.com/link.phpurl。
That said, it seems from your question that you are using relative links on purpose... do you have a reason not to use absolute links?
也就是说,从您的问题看来,您是故意使用相对链接的……您有理由不使用绝对链接吗?

