Trailing Slash for URLs


(Wil14son) #1

For SEO purposes, it is recommended to have all non-filename URLS (directories or prettified "permalink" URLs to borrow a Wordpress phrase) end with an ending slash.

What happens is that Google thinks of, for example, http://www.excellenc…m.sg/promotions and http://www.excellenc…sg/promotions/ as different pages.

It's also suggested that the / helps with performance due to the way Apache works.

On Yii, using the CUrlManager pattern rewrites, i'm able to make my application links have a trailing slash by adding in "urlSuffix"="/" in the urlManager configuration. However this doesn't solve the issue as if i hit, for example,

http://www.foo.com/about, it doesn’t redirect me to one with the ending slash.

I'm unable to use regular means of mod_rewrite redirection due to Yii using the index.php entry script.

Does anybody have any idea how the abovementioned could be done cleanly in Yii?


(Y!!) #2

I did this quick fix that works for me. Put into your applications components folder and make sure to extend your controllers from this class (eg "NewsController extends BaseController"):

<?php





class BaseController extends CController


{





	public function init()


	{


	


		parent::init();


		


		// Force traling slash to request URI


		$requestUri = yii::app()->request->requestUri;


		


		if (false === strpos($requestUri, '?') && '/' !== substr($requestUri, strlen($requestUri) - 1, 1))


		{


			yii::app()->request->redirect("{$requestUri}/", true, 301);


		}


		elseif ('/' !== substr($requestUri, strpos($requestUri, '?') - 1, 1))


		{


			yii::app()->request->redirect(substr($requestUri, 0, strpos($requestUri, '?')) . '/' . substr($requestUri, strpos($requestUri, '?')), true, 301);


		}


			


	}





}





?>

(Y!!) #3

I've modified this little script so it also removes double slashes from the request URI now.

<?php





class BaseController extends CController


{





	public function init()


	{


	


		parent::init();


		


		// Remove any double slashes and force a trailing slash to the request URI


		$requestUri = yii::app()->request->requestUri;


		$repairedRequestUri = $requestUri;


		


		while (false !== strpos($repairedRequestUri, '//'))


		{


			$repairedRequestUri = preg_replace("////", '/', $repairedRequestUri);


		}


		


		if (false === strpos($repairedRequestUri, '?') && '/' !== substr($repairedRequestUri, strlen($repairedRequestUri) - 1, 1))


		{


			$repairedRequestUri = "{$repairedRequestUri}/";


		}


		elseif ('/' !== substr($repairedRequestUri, strpos($repairedRequestUri, '?') - 1, 1))


		{


			$repairedRequestUri = substr($repairedRequestUri, 0, strpos($repairedRequestUri, '?')) . '/' . substr($repairedRequestUri, strpos($repairedRequestUri, '?'));


		}


		


		if ($repairedRequestUri !== $requestUri)


		{


			yii::app()->request->redirect($repairedRequestUri, true, 301);


		}


	


	}





}





?>

(Jerryablan) #4

Instead of this:



'/' !== substr($requestUri, strlen($requestUri) - 1, 1)


Try this:



'/' !== $requestUri{strlen($requestUri) - 1}


It’s faster than calling substr. :)