Connecting the dots…

Thoughts on Web and Personal Development

Creating clean SEO urls in symfony

with one comment

Being the symfony fan that I am, I have decided to start documenting some of the features I think are quite neat in the framework. So starting today, here’s a quick quite to writing a Search Engine Optimized URL.
So I am working on a project where users will be posting university details and I wanted the university names to appear on the url instead of the id. The problem with ID’s is it gives too much information about the application and if not carefully design can lead to data being exposed which the development might have not intented do.

So here’s the url that I was looking for. 

1
http://testsite.rajatpandit.com/university/Indian_institute_of_management_ahemdabad.html

I got to the point where I had functional code which gave me URLs like these:

1
http://testsite.rajatpandit.com/university/id/1

These are the steps to convert the URLs are humanly understandable.
Step 1:
Add additional field to the university table to store the modified name. So update the schema to addt the additional field.

1
2
3
4
5
6
7
8
9
10
11
12
  university:
    _attribute: {phpName: University}
    id: {type: integer, required: true, primaryKey: true, autoincrement: true}
    name: varchar(256)
    stripped_name: varchar(256)
    website: varchar(256)
    country: varchar(2)
    city: varchar(256)
    body: longvarchar
    approved: boolean
    created_at:
    updated_at:

Obviously you would need to do the regular to regenerate the models.

1
2
3
#symfony propel-build-model
#symfony propel-build-sql
#symfony propel-insert-sql

or do it all in one easy step.

1
#symfony propel-build-all

Step 2:
Now modify the model to update the new column (stripped_name) every time the standard setName($v) is called. For that we need to have a helper function that does this. Here’s the code I used, you can use your own version.

1
2
3
4
5
6
7
8
function stripText($text) {
  $text = strtolower($text);
  $text = preg_replace('/\W/', ' ', $text);
  $text = preg_replace('/\ +/', '_', $text);
  $text = preg_replace('/\-$/', '', $text);
  $text = preg_replace('/^\-/', '', $text);
  return $text;
}

I create a new file called CustomHelper.php and store in under lib/helper/ and then update the model class for University to override the function setName($v) with the following function.

1
2
3
4
5
6
  public function setName($v)
  {
    include_once('lib/helper/CustomHelper.php');
    parent::setName($v);
    $this->setStrippedName(stripText($v));
  }

That will ensure that the new column stripped_name gets updated.

Step 3:
Update your php template where you create the links to the following code.

1
2
3
4
5
6
7
<ul>
  <?php foreach($universities as $university): ?>
    <li>
      <?php echo link_to($university->getName(), '@university_display?stripped_name=' . stripText($university->getName())); ?>
    </li>
  <?php endforeach; ?>
</ul>

and update the routing.yml to match the routing name used.

1
2
3
article_display:
  url: /article/:stripped_title.html
  param: {module: home, action: article}

The routing rule reads out like this,
When you see the url containing /article/<something>.html redirect it to module: home to its action article and assign a request parameter <stripped_html> to <something>.

and that’s all you need to do to get your urls look all nice and clean.

Share:
  • Digg
  • del.icio.us
  • Google
  • Sphinn
  • Facebook
  • Mixx
  • LinkedIn
  • Ma.gnolia
  • StumbleUpon
  • TwitThis
  • Yahoo! Buzz

Written by rp

June 18th, 2008 at 4:22 am

One Response to 'Creating clean SEO urls in symfony'

Subscribe to comments with RSS or TrackBack to 'Creating clean SEO urls in symfony'.

  1. Well looks nice but saves no work for me, i have to add the name character even to any link i do.
    Even selecting the Selection of University is getting less nice cause of using name und not just simply retriving by PK.
    I think better way would be to change the stuff to using primary key and force linking with name, maybe i gonna try something and post it if i succeed.

    Johny

    28 Jul 08 at 5:26 am

Leave a Reply