PHP,htaccess的:在网址申请网页标题网址、标题、网页、PHP

2023-09-02 00:42:10 作者:爱意藏眼底

我想申请页面的HTML标题中的网址

I want to apply the page HTML title in the URL

例如,在这里(计算器)的网址是类似的东西:

for example in here (stackoverflow) the url is something like that:

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

您可以看到获得的非标题的-A-页面URL其中一部分是网页标题

you can see the "get-the-title-of-a-page-url" part which is the page title

我的意思是,当用户去spowpost.php?帖子= 1

what i mean is when the user go to spowpost.php?post=1

在实际的URL,显示了当页面加载会 spowpost.php?帖子= 1 $标题= .. the_title ..

the actual url that shows up when the pages load will be spowpost.php?post=1$title=..the_title..

我怎么能做到这一点?

编辑:我在想htaccess的,但我不知道这很舒服,所以教程将有助于该某种情况下。

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. For example:

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

您的PHP页面(的index.php)接收 $ id和标题作为参数_ GET []

Your PHP page (index.php) receives the id and title as parameters in $_GET[]:

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

您可以再使用(或ID,这是比较容易),以从数据源中检索正确的项目:

You can then use that (or the id, which is easier) to retrieve the correct item from your data source:

// 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采取行动。

Assuming you do have an id parameter in the URL of some sort, it's easier to query based on that value. The title portion can be used really only to make a readable URL, without needing to act on it in PHP.

在反向为标题转换为格式样这样使用的功能于网址,如下:

In reverse, to convert the title to the format-like-this-to-use-in-urls, do:

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

以上假设你的职称不包括是非法的网址...

The above assumes your titles don't include any characters that are illegal in a URL...

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

或者饲料为.htaccess:

Or to feed to .htaccess:

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