Last active
December 28, 2015 13:59
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 <sam@rmcreative.ru> | |
*/ | |
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); | |
} | |
} |
Для увеличения производительности предлагаю
if (Yii::app()->urlManager->showScriptName === false && strpos($requestUri,'index.php')!==false) {
preg_match('~^/index\.php(.*)~', $requestUri, $matches);
Yii::app()->request->redirect($matches[1], true, 301);
}
А чьё лучше? ;) https://gist.github.com/keltstr/7515215
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save as
protected/components/DuplicateFilter.php
.