Skip to content

Instantly share code, notes, and snippets.

@ergoz
Forked from samdark/DuplicateFilter.php
Created April 28, 2014 05:51
Show Gist options
  • Save ergoz/11362702 to your computer and use it in GitHub Desktop.
Save ergoz/11362702 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 <[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