简体   繁体   English

PHP,htaccess:在URL中应用页面标题

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

I want to apply the page HTML title in the URL 我想在URL中应用页面HTML标题

for example in here (stackoverflow) the url is something like that: 例如在这里(stackoverflow)url是这样的:

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

you can see the "get-the-title-of-a-page-url" part which is the page title 你可以看到“get-the-title-of-page-url”部分,它是页面标题

what i mean is when the user go to spowpost.php?post=1 我的意思是当用户去spowpost.php?post = 1

the actual url that shows up when the pages load will be spowpost.php?post=1$title=..the_title.. 当页面加载时显示的实际网址是spowpost.php?post = 1 $ title = .. the_title ..

how can i do that? 我怎样才能做到这一点?

EDIT: i was thinking about htaccess , but i don't know this very well so tutorial would help for this CERTAIN case.. 编辑:我正在考虑htaccess,但我不太了解这个教程将有助于这个例子。

You can use .htaccess for this. 你可以使用.htaccess。 For example: 例如:

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

Your PHP page (index.php) receives the id and title as parameters in $_GET[] : 您的PHP页面(index.php)在$_GET[]接收id和title作为参数:

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

You can then use that (or the id, which is easier) to retrieve the correct item from your data source: 然后,您可以使用它(或更容易的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';

Assuming you do have an id parameter in the URL of some sort, it's easier to query based on that value. 假设您在某种URL中确实有一个id参数,则更容易根据该值进行查询。 The title portion can be used really only to make a readable URL, without needing to act on it in PHP. 标题部分实际上只能用于制作可读的URL,而无需在PHP中对其进行操作。

In reverse, to convert the title to the format-like-this-to-use-in-urls , do: 相反,要将标题转换为format-like-this-to-use-in-urls ,请执行以下操作:

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

The above assumes your titles don't include any characters that are illegal in a URL... 以上假设您的标题不包含任何在URL中非法的字符...

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

Or to feed to .htaccess: 或者输入.htaccess:

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

From what I understand, your title will be passed to the page as part of the URL. 根据我的理解,您的标题将作为URL的一部分传递到页面。 To show it in the title bar, put this in the section: 要在标题栏中显示它,请将其放在以下部分中:

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

You might need to change parts of this, for instance dashes to spaces or something. 您可能需要更改此部分,例如破折号到空格或其他内容。 If that is the case, use PHP's str_replace function: http://php.net/str_replace 如果是这种情况,请使用PHP的str_replace函数: http//php.net/str_replace

Not sure about what's the problem you are facing but just according to what you say in your post the anwser would be: 不确定你面临的问题是什么,但根据你在帖子中所说的,anwser会是:

1.You take the ID from the URL. 1.您从URL中获取ID。
2.You search in your database for the original title 2.在数据库中搜索原始标题
3. And then display it in the tag in the of your HTML. 3.然后将其显示在HTML的标记中。

Clarify if you have problem with any of the previous points. 澄清您是否有任何前面的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM