Skip to content

Instantly share code, notes, and snippets.

@samdark
Last active December 28, 2015 13:59
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save samdark/7511823 to your computer and use it in GitHub Desktop.
Save samdark/7511823 to your computer and use it in GitHub Desktop.
<?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);
}
}
@samdark
Copy link
Author

samdark commented Nov 17, 2013

Save as protected/components/DuplicateFilter.php.

@dimanus
Copy link

dimanus commented Nov 21, 2013

Для увеличения производительности предлагаю

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);
        }

@githubjeka
Copy link

А чьё лучше? ;) https://gist.github.com/keltstr/7515215

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment