Infinite trailing slashes problem

Trailing slashes get ignored by Yii. For example:


http://www.domain.com/dir1/ 

works, but so does


http://www.domain.com/dir1//////////////

where it shouldn’t

Same goes for files


http://www.domain.com/file.html

works, but so does


http://www.domain.com/file.html/

where it shouldn’t, as does


http://www.domain.com/file.html//////////////

where it shouldn’t

In those cases, it should just return a 404 error.

How can we prevent this? I’m proudly using Yii 1.1.7 with the following config for my urlmanager:




'urlManager'=>array(

'urlFormat'=>'path',

'showScriptName'=>false,

'useStrictParsing'=>true,

'urlSuffix'=>'/',

'caseSensitive'=>false,

'rules'=>array(

...

'<view:[a-zA-Z0-9-]+>.html' => 'site/page',

...

))   




‘urlSuffix’=>’/’,

this is VERY strange setting… it should rather be sth like this:

‘urlSuffix’=>’.html’,

as this specify what can be appended to your url…

I did that to automatically suffix all links with ‘/’, but even when I remove that, additional slashes are still considered valid by Yii.

I would say this behavior comes from CUrlManager::parsePathInfo. It seems to simply skip over empty path segments. I’m not sure if this introduces big problems with SEO, but if you’re worried, you can always extend CUrlManager and override this method. Maybe send a 301 - Moved Permanently response with a cleaned up version of the requested URL and call Yii::app()->end().

This is not a Yii thing… try to call any PHP script (non-Yii) and add the slashes…

You can add rel=canonical to all your pages if you think it will cause SEO problems.

I wrote EUrlRule class, that solves this problem except multiply slashes at the end of URI:


class EUrlRule extends CUrlRule

{

	static private $requestUri = null;


	public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)

	{

		$urlSuffix = $this->urlSuffix === null ? $manager->urlSuffix : $this->urlSuffix;

		$pathInfo = $manager->removeUrlSuffix($rawPathInfo, $this->urlSuffix);


		// URL suffix required, but not found in the requested URL

		if($pathInfo===$rawPathInfo && !empty($urlSuffix) && $urlSuffix!='/') {

			return false;

		}


		if (is_null(self::$requestUri)) {

			self::$requestUri = $request->getRequestUri();

			if (($pos = strpos(self::$requestUri, '?')) !== false)

				self::$requestUri = substr(self::$requestUri, 0, $pos);

		}

		// urlSuffix isn't actual suffix (if requestUri contains trailing /)

		if (substr(self::$requestUri, -1*strlen($urlSuffix)) != $urlSuffix)

			return false;


		return parent::parseUrl($manager, $request, $pathInfo, $rawPathInfo);

	}

}



You can improve it with checking for multiply slashes