Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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

This comment has been minimized.

Copy link
Owner Author

samdark commented Nov 17, 2013

Save as protected/components/DuplicateFilter.php.

@dimanus

This comment has been minimized.

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

This comment has been minimized.

Copy link

githubjeka commented Nov 21, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.