我想让我的链接SEO。在我开始之前,链接看起来像这样:
http://www.domain.tld/index.php?page=blog
我的目标是将其更改为:http://www.domain.tld/blog
。现在有效。
我将htaccess更改为:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\w+$ index.php?page=$0 [L]
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{QUERY_STRING} ^page=(\w+)$
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteRule ^blog/(\d+)-([\w-]+)$ index.php?page=single_news&id=$1&headline=$2
现在我想更改任何博客条目标题的URI。它们看起来像这样:http://www.domain.tld/index.php?page=single_news&id=2&headline=This%20Is%20A%20Headline
我想让它们看起来像这样:http://www.domain.tld/blog/2-this-is-a-headline
我在div-class“news_headline”中生成我的标题链接(见下文)。
<div id="main">
<?php
$query = "SELECT `id`, `headline`, `writer`, `content`, DATE_FORMAT(date,\"%d. %M %Y\") AS `date` FROM `blog` ORDER BY `id` DESC";
$news_resource = mysql_query($query) or die(mysql_error());
?>
<?php
$all_news = array();
$five_news = array();
for($i = 0; $news = mysql_fetch_object($news_resource); $i++){
if($i < 5){
$five_news[] = $news;
}
$all_news[] = $news;
}
?>
<?php foreach($five_news as $news)
{ ?>
<div class="news_bg">
<div class="news_headline"><a href="blog/<?php echo $news->id; ?>-<?php echo $news->headline; ?>"><?php echo $news->headline; ?></a></div>
<div class="news_infoline_top"><?php echo $news->date; ?> · <?php echo $news->writer; ?></div>
<div class="news_text"><?php echo $news->content; ?></div>
</div>
<?php } ?>
</div>
使用我的htaccess(见上文)链接就像这样:
http://www.domain.tld/blog/2-This Is A Headline
我已经有了这方面的帮助,一个好人给了我这段代码片段,使链接看起来像我想要的,但我不知道如何使用它们:
$urititle = strtolower(preg_replace('/[^\w-]+/','-', $title));
和
$_GET['headline'] != $urititle
我迷路了。
答案 0 :(得分:1)
你已经非常接近了:
您还有一些步骤可以完成此操作。索姆注意到:
$assigned = sscanf($slug, '%d-', $id);
就是这样!