問(wèn)題描述
我希望答案很簡(jiǎn)單,我會(huì)想哭,但我似乎無(wú)法弄清楚.我是 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*/*(帶有尾部斜杠)并且它工作正常,但是每當(dāng)我轉(zhuǎn)到鏈接,它將新鏈接附加到 url 的后面.例如,我有一個(gè)關(guān)于和聯(lián)系鏈接.如果我點(diǎn)擊關(guān)于它導(dǎo)航到 domain.com/about/然后如果我點(diǎn)擊聯(lián)系人,它導(dǎo)航到 domain.com/about/contact/并將繼續(xù)添加鏈接到 url 的末尾.如果我在 domain.com 并單擊一個(gè)鏈接(在這種情況下是關(guān)于),它將轉(zhuǎn)到 domain.com/about/,如果我再單擊大約 4 次,我的地址欄將顯示domain.com/about/about/about/about/about/" 我在下面的一個(gè)非常簡(jiǎn)單的例子中復(fù)制了這個(gè),我做錯(cuò)了什么?
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'];
}
?>
感謝您的幫助!
如果我使用絕對(duì)路徑,它可以正常工作,但如果我不是絕對(duì)必須,我寧愿不要.
edit: It works okay if I use an absolute path, but I'd rather not if I don't absolutely have to.
edit2:添加
RewriteBase /
斷開(kāi)鏈接.他們似乎要去 domain.com/about/和 .../contact/,但我得到 404 - 我假設(shè)我使用的規(guī)則在某種程度上與我進(jìn)行鏈接的方式不兼容,這就是為什么我也包括 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.
推薦答案
您正在定義相對(duì)于當(dāng)前路徑的 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>
成為(注意網(wǎng)址上的前導(dǎo)/):
becomes (note the leading / on the urls):
<a href="/about/">about</a> | <a href="/contact/">contact</a><br><br>
當(dāng)您在頁(yè)面 site.com/about/us
上時(shí),瀏覽器將像 <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,以在網(wǎng)址中使用絕對(duì)路徑,而不是像現(xiàn)在這樣使用相對(duì)路徑.
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.
剛剛注意到您的編輯.你真的應(yīng)該使用絕對(duì)路徑,而不是相對(duì)路徑.如果您想保留相對(duì) URL,則必須在所有頁(yè)面上使用類(lèi)似 <base href="/"/>
的內(nèi)容.
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.
這篇關(guān)于如何阻止 mod_rewrite 將鏈接附加到 url?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!