簡體   English   中英

<a>將“href”內容連接到現有 URL 的</a>HTML<a>鏈接</a>

[英]HTML <a> link concatenating the 'href' content to existing URL

到目前為止,我一直在使用 GET 參數在我的網站(托管在我大學的服務器上)的頁面之間進行路由。 我想使用$_SERVER["PATH_INFO"]代替,因為它是在頁面之間路由的更簡潔的方式。

我想從:

www.universitydomain.subdomain/myProject/index.php?action=imageGallery

到:

www.universitydomain.subdomain/myProject/index.php/imageGallery

我有 3 頁:

  1. 我的主頁,index.php
  2. 我的圖片庫頁面,當前可在 GET 參數action等於“imageGallery”時訪問
  3. 將圖像添加到我的畫廊的頁面,可在 GET 參數action等於“addImage”時訪問

每個頁面上都有一個由<a>鏈接組成的菜單:

<a href="index.php" > Main page </a>
<a href="?action=imageGallery" > Gallery </a>
<a href="?action=addImage" > Add image </a>

這樣做時,一切正常。 所以我嘗試使用PATH_INFO樣式的 URL,如下所示:

<a href="index.php" > Main page </a>
<a href="index.php/imageGallery" > Gallery </a>
<a href="index.php/addImage" > Add image </a>

如果我在圖庫頁面上,我希望<a>Main pagey</a>鏈接在我的 URL 中寫入index.php ,這將使我回到主頁。 然而,這發生了:

  • 我在index.php URL 是www.universitydomain.subdomain/myProject/index.php
  • 我點擊第二個鏈接進入畫廊。 URL 變為www.universitydomain.subdomain/myProject/index.php/imageGallery
  • 我單擊第一個鏈接返回主頁面。 URL 變為www.universitydomain.subdomain/myProject/index.php/index.php
  • 在第三步,如果不是返回主頁面,而是單擊第三個鏈接進入addImage頁面,則 URL 變為www.universitydomain.subdomain/myProject/index.php/index.php/addImage

這是為什么 ? 我無法重現我的問題的最小示例,因此它似乎與我的項目有關(不幸的是,該項目太大而無法包含在本文中)。 即使無法重現這個問題,你們中的一些人也可能與它相關並有一些建議。

我的印象是,在進入圖庫頁面(其 URL /myProject/index.php/imageGallery )后,我頁面的“參考路徑”已從/myProject/更改為/myProject/index.php/和因此,當點擊<a href="index.php">鏈接時,它會將我重定向到/myProject/index.php/index.php而不是/myProject/index.php/

當您使用單個index.php端點(有時也稱為Front Controller )將 URL-Layout 從查詢信息部分( ?action=<page-name> )遷移(更改)到路徑部分(在 PHP 中通常使用$_SERVER['PATH_INFO'] )並且您引入了額外的路徑組件,您可以看到這種行為。 這是 URL 解析規則的標准。

query-info               ->  path                      effect

index.php -or- ?, ., ""  ->  index.php                 0 new path components
?action=imageGallery     ->  index.php/imageGallery    + 1 new path component
?action=addImage         ->  index.php/addImage        + 1 new path component

在您緩解遷移的非常具體的情況下,您可能需要考慮快速解決方法:如果沒有給出PATH_INFO ,則重定向到與其他兩個頁面具有相同路徑組件數量的主頁。

if (empty($_SERVER['PATH_INFO'])) {
   header('location: index.php/mainPage');
   echo '<!DOCTYPE html><title>moved</title><h1>moved</h1><a href="index.php/mainPage">here</a>.';
   return;
}
<a href="mainPage" > Main page </a>
<a href="imageGallery" > Gallery </a>
<a href="addImage" > Add image </a>

或類似的,代替index.php/mainPageindex.php/這可能對你的情況下更好的語義:

<a href="./" > Main page </a>
<a href="./imageGallery" > Gallery </a>
<a href="./addImage" > Add image </a>

對於最后兩個, ./前綴嚴格來說是不必要的,這是等價的:

<a href="./" > Main page </a>
<a href="imageGallery" > Gallery </a>
<a href="addImage" > Add image </a>

這可能會讓您在保持站點正常運行的同時更好地了解 URL 布局更改的后果。

對於底層的基本理解,我強烈建議研究 URL 解析規范,因為這是非常基礎的東西,它將幫助您做出有關 URL 布局更改的決定,以及您希望如何使用您的 HTML 文檔以及您的服務器端腳本和整體應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM