-
-
Save ergoz/11362702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* DuplicateFilter prevents Yii from exposing URLs starting with /index.php/ when showScriptName is false. Such | |
* URLs are automatically redirected to proper ones. | |
* | |
* To use add the following to your controller: | |
* | |
* ```php | |
* public function filters() { | |
* return array( | |
* 'accessControl', // perform access control for CRUD operations | |
* array('DuplicateFilter'), | |
* ); | |
* } | |
* ``` | |
* | |
* @author Alexander Makarov <[email protected]> | |
*/ | |
class DuplicateFilter extends CFilter | |
{ | |
/** | |
* Performs the pre-action filtering. | |
* @param CFilterChain $filterChain the filter chain that the filter is on. | |
* @return boolean whether the filtering process should continue and the action | |
* should be executed. | |
*/ | |
protected function preFilter($filterChain) | |
{ | |
$requestUri = Yii::app()->request->requestUri; | |
if (Yii::app()->urlManager->showScriptName === false && preg_match('~^/index\.php(.*)~', $requestUri, $matches)) { | |
Yii::app()->request->redirect($matches[1], true, 301); | |
} | |
return parent::preFilter($filterChain); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment