簡體   English   中英

PHP,htaccess:在URL中應用頁面標題

[英]PHP , htaccess: apply page title in URL

我想在URL中應用頁面HTML標題

例如在這里(stackoverflow)url是這樣的:

http://stackoverflow.com/questions/10000000/get-the-title-of-a-page-url

你可以看到“get-the-title-of-page-url”部分,它是頁面標題

我的意思是當用戶去spowpost.php?post = 1

當頁面加載時顯示的實際網址是spowpost.php?post = 1 $ title = .. the_title ..

我怎樣才能做到這一點?

編輯:我正在考慮htaccess,但我不太了解這個教程將有助於這個例子。

你可以使用.htaccess。 例如:

RewriteEngine On
RewriteRule ^questions/(\d+)/([a-z-]+) index.php?id=$1&title=$2 [L]

您的PHP頁面(index.php)在$_GET[]接收id和title作為參數:

echo $_GET['title'];
// get-the-title-of-a-page-url

然后,您可以使用它(或更容易的id)從數據源中檢索正確的項目:

// Assuming you didn't store the - in the database, replace them with spaces
$real_title = str_replace("-", " ", $_GET['title']);
$real_title = mysql_real_escape_string($real_title);

// Query it with something like
SELECT * FROM tbl WHERE LOWER(title) = '$real_title';

假設您在某種URL中確實有一個id參數,則更容易根據該值進行查詢。 標題部分實際上只能用於制作可讀的URL,而無需在PHP中對其進行操作。

相反,要將標題轉換為format-like-this-to-use-in-urls ,請執行以下操作:

$url_title = strtolower(str_replace(' ', '-', $original_title));

以上假設您的標題不包含任何在URL中非法的字符...

$article_link = "http://example.com/spowpost.php?post=$postid&$title=$url_title";

或者輸入.htaccess:

$article_link = "http://example.com/spowpost$postid/$url_title";

根據我的理解,您的標題將作為URL的一部分傳遞到頁面。 要在標題欄中顯示它,請將其放在以下部分中:

<?php $title=urldecode($_GET["title"]); echo "<title>$title</title>"; ?>

您可能需要更改此部分,例如破折號到空格或其他內容。 如果是這種情況,請使用PHP的str_replace函數: http//php.net/str_replace

不確定你面臨的問題是什么,但根據你在帖子中所說的,anwser會是:

1.您從URL中獲取ID。
2.在數據庫中搜索原始標題
3.然后將其顯示在HTML的標記中。

澄清您是否有任何前面的問題。

暫無
暫無

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

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