Seo Friendly Urls

Hello!

My Yii powered website has been live for about a month now (http://tasta.be) and it’s going pretty good. Right now, I’m heavily invested in SEO. I’ve managed to make my URLs a little bit more URL friendly: http://tasta.be/videos/20 for example, but then I got stuck.

I got the documentation and read all of it, but I think I’m currently not making the click to wrap my head around it. There are two things I want to accomplish, but I fail trying to do so:

Maybe this is a lot to ask, but without help I don’t think I’m ever going to achieve what I want. My current .htaccess looks like this:


RewriteEngine on


# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d


# otherwise forward it to index.php

RewriteRule . index.php

If somebody could help me, I would be so grateful. If I manage to get it working, I’ll give away some Steam codes for Dungeonland and The Showdown Effect as a token of my appreciation. :)

Hello Nicolas, welcome to the forum.

I don’t have much experience in this area, but it seems that “Custom URL Rule Class” is exactly what you need.

http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes

I think you can easily implement your own.

BTW, nice site. :)

It sounds like what you are trying to do should be fairly simple

a rule like ‘videos/<id:\d+>’ => ‘videos/view’ should get you what you have now.

To change to your new format you would want to add a new rule before it like (the .+ can be replaced by a more specific regex if appropriate like [A-Za-z0-9_-]+).

‘videos/<id:\d+>-<urlfield:.+>’ => ‘videos/view’

As for the redirects, here is the way I did that. Each relevant action calls this function near it’s start to redirect if necessary. The $url parameter can be created with Yii::app()->createUrl() as you would creating links to it anywhere else.




	/**

	 * Redirect to the specified URL if we are not already there.

	 * 

	 * This is necessary for SEO optimization (search engines penalize for duplicate content at different URLs).

	 * Duplicate URLs will happen because of stuff like picture/story titles changing, category hierarchy changes, 

	 * and simply all the old URLs that are redirected to the new ones. 

	 * 

	 * @param String $url The correct URL for this page

	 */

	function redirectCanonicalURL($url)

	{

		if(Yii::app()->request->requestUri != $url)

		{	

			//header("HTTP/1.1 301 Moved Permanently"); 

			header("Location: $url", true, 301);

			Yii::app()->end();

		}		

	}



Softark’s method would also work and is more powerful, but is probably less efficient if the simple url rules do what you need.

Not doing the redirect, but using canonical url tags should be good enough for google:


<link rel="canonical" href="http://example.com/" />

Yes, Cstdenis’s proposal looks quite reasonable. :)

I think usually we don’t need to hide “id”.