How to hide `index` action from URL

You are viewing revision #1 of this wiki article.
This is the latest version of this article.

Here is my experiment inspired by my own question and others from the forum.

I am not talking about [[Url: hide index.php](http://www.yiiframework.com/wiki/214/url-hide-index-php "this")] or [[How to hide index.php on nginx]](http://www.yiiframework.com/wiki/15/how-to-hide-index-php-on-nginx "this"), however, i am also talking about 'hide' something.

take yii default scaffolding web app as example: assuming that you already did the trick as above to hide index.php from your uri, you might notice that the following two URLs are still working.

/webroot/site/index
/webroot/index.php/site/index

someone in the forum suggested add the following code in /protected/components/Controller.php

public function init(){
	if (strpos(Yii::app()->request->requestUri, '/index.php') !== false){
		throw new CHttpException(404,'The requested page does not exist.');
	}
}

I am not sure if I like the idea of throwing an error, so I took another approach [redirect], so modified the code as:

public function init(){
	if (Yii::app()->urlManager->showScriptName == false){
		if (strpos(Yii::app()->request->requestUri, '/index.php') !== false){
			$_uri = str_replace("/index.php", "", Yii::app()->request->requestUri);
			$_uri = str_replace("//", "", $_uri);
			$this->redirect($_uri);
		}
	}
}

then you will always see url like this which 'truelly' hide index.php

/webroot/site/index

good enough? just wait a minute, someone mentioned in the forumn he/she does not really like to see 'index' in this url even! he/she expects this:

/webroot/site

hmmmm, would redirect do the trick? let's put some code in /protected/controller/SiteController.php:

protected function beforeAction($action)
{
	$_uri = false;
	if (Yii::app()->urlManager->showScriptName == false){
		if (strpos(Yii::app()->request->requestUri, '/index.php') !== false){
			$_uri = str_replace("/index.php", "", Yii::app()->request->requestUri);
		}
		if (Yii::app()->controller->action->id == 'index'){
			if (!$_uri) {
				if (strpos(Yii::app()->request->requestUri, "/index") !== false){
					$_uri = str_replace("/index", "", Yii::app()->request->requestUri);
				}
			} else {
				if (strpos($_uri, "/index") !== false){
					$_uri = str_replace("/index", "", $_uri);
				}
			}
		}
	}
		
	if ($_uri !== false){
		$this->redirect($_uri);
	}
	return parent::beforeAction($action);
}

give a try, works? yeah ,,,

*note:

I'm not sure if this is 'the' solution, but sure it will work for those 'picky' developers or users for a clean url.

You may ask why don't you have the above code in Contoller Class [/protected/components/Controller.php], so this trick works for all Controllers? I tried, did work. I guess the redirection lost its way to hit SiteController. Someone may help me on this.

and feel free to modify this wiki.

3 1
9 followers
Viewed: 25 274 times
Version: Unknown (update)
Category: How-tos
Tags: index
Written by: rootbear
Last updated by: rootbear
Created on: Jun 20, 2012
Last updated: 12 years ago
Update Article

Revisions

View all history