Deal of the Day

Home » Main » Manning Forums » 2007 » Zend Framework in Action

Thread: SEO and Url Rewriting

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 5 - Pages: 1 - Last Post: Mar 19, 2008 10:57 PM by: nicklo
geetarista

Posts: 15
From: Arizona
Registered: 2/20/08
SEO and Url Rewriting
Posted: Mar 18, 2008 3:04 PM
  Click to reply to this thread Reply

What is the best way of handling SEO and url rewriting in Zend Framework in general?

For example, I know '/controller/action/' is preferred by Google et al. over '/controller/action' and I know that it saves a fraction of time in Apache. What about changing something from '/news/view/id/25/' to '/news/view/some-cool-title-of-a-news-story' or something along those lines. Is this better handled by tweaking code, .htaccess, or something else?

Has anybody run into this yet or is there another place I should look?

Thanks!

nicklo

Posts: 43
From: Australia
Registered: 5/25/07
Re: SEO and Url Rewriting
Posted: Mar 18, 2008 11:44 PM   in response to: geetarista in response to: geetarista
  Click to reply to this thread Reply

Hi Geetarista,

Short answer is to use Zend_Controller_Router_Rewrite.

I'm going to hesitatingly add a rough example here lifted from an older project and admit it may not work as is. The project I'm currently working on is hiding from Google, etc, so has no need for nice URLs. With that disclaimer in mind:

Some routes in an ini format config file:

routes.article.route = article/:ref
routes.article.defaults.controller = article
routes.article.defaults.action = index

In your bootstrap:

$router = new Zend_Controller_Router_Rewrite;
$route_config = new Zend_Config_Ini( 'configuration/routes.ini', 'development' );
$router->addConfig( $route_config, 'routes' );
$frontController->setRouter($router);
$frontController->dispatch();

In an ArticleController action controller file:

public function indexAction()
{
$filter_strip_tags = new Zend_Filter_StripTags();
$ref = $filter_strip_tags->filter($this->_getParam('ref'));
include_once 'models/ArticleTable.php';
$articleTable = new ArticleTable;
$where = $articleTable->getAdapter()->quoteInto('ref = ?', $ref);
$row = $articleTable->fetchRow($where);
$this->view->article = $row;
}

The "ref" is the article reference name so could be your example "some-cool-title-of-a-news-story".

Hopefully that's enough of a demo to show how much flexibility you have. If you need more rewriting then you can use the regex version.

More info is of course in the manual here:

http://framework.zend.com/manual/en/zend.controller.router.html

Hope that helps and feel free to correct me if any of the above is olde-worlde ZF code now.

Nick

geetarista

Posts: 15
From: Arizona
Registered: 2/20/08
Re: SEO and Url Rewriting
Posted: Mar 19, 2008 8:51 AM   in response to: nicklo in response to: nicklo
  Click to reply to this thread Reply

Cool--thanks Nick! I just didn't know which was the best way to handle it, though I can see this way is pretty easy.

One question though: I read somewhere a while back that ZF will not always require mod_rewrite. Is this the case yet with Z_C_Router_Rewrite or is mod_rewrite still required? Also, if and when this does happen, will you be able to control specific folders (if necessary) or will it be application wide?

Thanks again for your help!

-- Robby

nicklo

Posts: 43
From: Australia
Registered: 5/25/07
Re: SEO and Url Rewriting
Posted: Mar 19, 2008 4:27 PM   in response to: geetarista in response to: geetarista
  Click to reply to this thread Reply

Glad to be able to help.

On your question: I'd need to see where you read that really. The key role that mod_rewrite has is in directing requests to the index.php file (ie the front controller file, depending on your setup) eg:

# Rewrite rules for Zend Framework
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/xmlrpc/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

Zend_Controller_Router_Rewrite works independently of that (I could possibly be wrong there as I've never actually poked around in the source code but cannot see why it would need to).

I'm not sure what you mean by "be able to control" what is it you are aiming to do?

- NIck

geetarista

Posts: 15
From: Arizona
Registered: 2/20/08
Re: SEO and Url Rewriting
Posted: Mar 19, 2008 7:04 PM   in response to: nicklo in response to: nicklo
  Click to reply to this thread Reply

You know what, I'm totally confused now. I just tried to dig back and find where I read that, and I'm finding things that say all kinds of stuff about the subject. That's one of the "problems" I keep running in to: the ZF keeps getting so many improvements and changes that it can be hard to keep up with everything that's going on. That's definitely a good problem to have, though. But I think the main source was in Cal's book: "The Zend Framework can work without mod_rewrite in Apache but it requires the manual configuration of the routes." There are also many other things that I have read so I may have just gotten confused with it all.

I don't have a problem with mod_rewrite, but I was initially just trying to figure out if it was better to use mod_rewrite or Router_Rewrite as far as web crawlers are concerned. It would also be good for someone that needed to be able to have a distributed app where they didn't have to worry about whether the server was Apache with mod_rewrite, IIS with an addon, etc.

Here is an interesting post I found that goes in to quite a bit of detail about rewriting: http://www.chrisabernethy.com/zend-framework-legacy-scripts/

And as far as the part about controlling different directories, I'm thinking about major apps that use dynamic domains such as username.domain.com or where multiple apps are installed on one domain. I know it's a bunch of what ifs, but I've been running into some of these situations lately...

Anyway, thanks again for the help and keep up the good work with the book!

nicklo

Posts: 43
From: Australia
Registered: 5/25/07
Re: SEO and Url Rewriting
Posted: Mar 19, 2008 10:57 PM   in response to: geetarista in response to: geetarista
  Click to reply to this thread Reply

> That's one of the "problems" I keep running
> in to: the ZF keeps getting so many improvements and
> changes that it can be hard to keep up with
> everything that's going on.

Ha, yeah, imagine having to write a book about it all! Now you know why there are 3 of us.

> Here is an interesting post I found that goes in to
> quite a bit of detail about rewriting:
> http://www.chrisabernethy.com/zend-framework-legacy-sc
> ripts/

Aha, yeah that is a useful post. I have a site at the moment I need to convert to using ZF, thanks for pointing it out.

> And as far as the part about controlling different
> directories, I'm thinking about major apps that use
> dynamic domains such as username.domain.com or where
> multiple apps are installed on one domain. I know
> it's a bunch of what ifs, but I've been running into
> some of these situations lately...

Well feel free to post any questions you have.

Cheers,

Nick

Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff