如何阻止 mod_rewrite 将链接附加到 url?

How do I stop mod_rewrite from appending links to url?(如何阻止 mod_rewrite 将链接附加到 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)