[英]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.