1

Possible Duplicate:
Regular Expression Sanitize (PHP)

I want to make my links SEO. Before I started the links looked like this:

http://www.domain.tld/index.php?page=blog

My goal was to change it to: http://www.domain.tld/blog. That works now.

I changed the htaccess to this:

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

Now I want to change the URIs of the headlines of any blog entry. They looked like this: http://www.domain.tld/index.php?page=single_news&id=2&headline=This%20Is%20A%20Headline

I want to make them looking like this: http://www.domain.tld/blog/2-this-is-a-headline I am generating my headline-links in the div-class "news_headline" (see below).

<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; ?> &middot; <?php echo $news->writer; ?></div>
<div class="news_text"><?php echo $news->content; ?></div>
</div>
<?php } ?>
</div>

With my htaccess (see above) the links are like this now:

http://www.domain.tld/blog/2-This Is A Headline

I already had help about this and a good guy gave me this code-snippets to make the links looking like I want, but I don't know how to use them:

$urititle = strtolower(preg_replace('/[^\w-]+/','-', $title));

and

$_GET['headline'] != $urititle

I am lost.

Community
  • 1
  • 1
John Smith
  • 363
  • 2
  • 7
  • 21
  • Just some notes: 5 News is [`LIMIT 5 OFFSET 0` in SQL](http://dev.mysql.com/doc/refman/5.5/en/select.html). Query that from the database directly. Then a small mistake: You don't query *title* from the database but *headline*. Fix that. The next thing you need to do in your loop is to turn the title into it's [*"slug"*](http://en.wikipedia.org/wiki/Slug_(web_publishing)) which you should put into a function of it's own (like `slugify($news->title)`). – hakre Oct 26 '12 at 12:03
  • What exactly is your question?! – Salman A Oct 26 '12 at 12:10
  • …or you just use some existing function to do so, e.g. Textpatterns `dumbDown()` function, which "transliterates a string to ASCII". See: http://textpattern.googlecode.com/svn/development/4.x/textpattern/lib/txplib_misc.php – feeela Oct 26 '12 at 12:11
  • @SalmanA How do I get my links like this? `http://www.domain.tld/blog/2-this-is-a-headline` – John Smith Oct 26 '12 at 12:21
  • @hakre There is no _title_ in my database. The mistake was in my echo. I changed it to _headline_ now. Now the links looks like this `http://www.domain.tld/blog/2-This Is A Headline` – John Smith Oct 26 '12 at 12:24
  • Yes, thought so because before it was empty. Edit your question so that little mistake is removed already. Have you seen the link by @SilentGhost? I think that is what you're looking for. – hakre Oct 26 '12 at 12:26
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Oct 26 '12 at 12:31

1 Answers1

1

You are already pretty close:

  • You put the ID to the beginning of the intersting part. That allows you to query your database by the ID which is unique and fast.
  • You have the understanding that you need to transform the text of the headline into the slug variant of it and you already know that this is string processing.

You have some steps left to finish this. Somme notes:

  • Create yourself a function that does the string transformation. The important part here is, that you can call it simply.
  • When the request to an article comes in, fetch the article based on the ID value you obtain from the string: $assigned = sscanf($slug, '%d-', $id);
  • For SEO, do the same as when creating the link. Then compare if the current link still is correct (the headline might have changed). If not, do a permanent redirect to the correct link.

And that's it!

hakre
  • 193,403
  • 52
  • 435
  • 836