Yii Framework Forum: Seo Friendly Urls - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Seo Friendly Urls I got the documentation, I just can't get it to work Rate Topic: -----

#1 User is offline   NicolasVH 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-November 12

Posted 17 February 2013 - 05:57 AM

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. :)
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 17 February 2013 - 06:20 AM

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.yiiframew...rl-rule-classes
I think you can easily implement your own.

BTW, nice site. :)
0

#3 User is offline   Cstdenis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 22-March 11

Posted 18 February 2013 - 01:40 AM

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.
1

#4 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 315
  • Joined: 08-June 10
  • Location:France

Posted 18 February 2013 - 02:18 AM

Not doing the redirect, but using canonical url tags should be good enough for google:
<link rel="canonical" href="http://example.com/" />

0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 18 February 2013 - 06:11 PM

Yes, Cstdenis's proposal looks quite reasonable. :)
I think usually we don't need to hide "id".
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users