How do I stop mod_rewrite from appending links to url?(如何阻止 mod_rewrite 将链接附加到 url?)
问题描述
我希望答案很简单,我会想哭,但我似乎无法弄清楚.我是 mod_rewrite 的新手.
I expect the answer is going to be something so simple I'll want to cry, but I can't seem to figure it out. I'm new to mod_rewrite.
我想将我的链接从 domain.com/?p=about 更改为 domain.com/about*/*(带有尾部斜杠)并且它工作正常,但是每当我转到链接,它将新链接附加到 url 的后面.例如,我有一个关于和联系链接.如果我点击关于它导航到 domain.com/about/然后如果我点击联系人,它导航到 domain.com/about/contact/并将继续添加链接到 url 的末尾.如果我在 domain.com 并单击一个链接(在这种情况下是关于),它将转到 domain.com/about/,如果我再单击大约 4 次,我的地址栏将显示domain.com/about/about/about/about/about/" 我在下面的一个非常简单的例子中复制了这个,我做错了什么?
I wanted to change my links from things like domain.com/?p=about to domain.com/about*/* (with the trailing slash) and it works fine, but whenever I move on to a link, it appends the new link to the back of the url. For example, I have an about and a contact link. If I click about it navigates to domain.com/about/ then if I click contact, it navigates to domain.com/about/contact/ and will keep adding the links to the end of the url. If I'm at domain.com and click a link(about, in this case) it will go to domain.com/about/ and if I click about 4 more times, my address bar is going to say "domain.com/about/about/about/about/about/" I have reproduced this in a very simple example below, what am I doing wrong?
.htaccess
RewriteEngine On
RewriteRule ([a-zA-Z0-9]+)/$ index.php?p=$1
index.php
<a href="about/">about</a> | <a href="contact/">contact</a><br><br>
<?php
if(!isset($_GET['p'])) {
echo "home";
} else {
echo $_GET['p'];
}
?>
感谢您的帮助!
如果我使用绝对路径,它可以正常工作,但如果我不是绝对必须,我宁愿不要.
edit: It works okay if I use an absolute path, but I'd rather not if I don't absolutely have to.
edit2:添加
RewriteBase /
断开链接.他们似乎要去 domain.com/about/和 .../contact/,但我得到 404 - 我假设我使用的规则在某种程度上与我进行链接的方式不兼容,这就是为什么我也包括 index.php.
breaks the links. They appear to be going to domain.com/about/ and .../contact/, but I get a 404 - I'm assuming the rule I used is somehow incompatible with the way I'm doing my linking, which is why I included index.php as well.
推荐答案
您正在定义相对于当前路径的 HTML 中的所有链接.
You are defining all of your links in HTML relative to the current path.
您需要更改链接,以便:
You will need to change your links such that:
<a href="about/">about</a> | <a href="contact/">contact</a><br><br>
成为(注意网址上的前导/):
becomes (note the leading / on the urls):
<a href="/about/">about</a> | <a href="/contact/">contact</a><br><br>
当您在页面 site.com/about/us
上时,浏览器将像 <a href="home/"
这样的链接解析为 <代码>site.com/about/us/home.
When you are on a page site.com/about/us
a link like <a href="home/"
gets resolved by the browser to be site.com/about/us/home
.
解决方案是更改所有链接、图像、样式表和 javascript,以在网址中使用绝对路径,而不是像现在这样使用相对路径.
The solution is to change all of your links, images, stylesheets, and javascripts to use absolute paths in your URLs, not relative ones like you have now.
刚刚注意到您的编辑.你真的应该使用绝对路径,而不是相对路径.如果您想保留相对 URL,则必须在所有页面上使用类似 <base href="/"/>
的内容.
Just noticed your edit. You really should use absolute paths, not relative ones. If you want to keep the relative URLs then you will have to use something like <base href="/" />
on all of your pages.
这篇关于如何阻止 mod_rewrite 将链接附加到 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何阻止 mod_rewrite 将链接附加到 url?
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 使用 PDO 转义列名 2021-01-01