FUEL has added a way to deal with page redirects that doesn't require the need for .htaccess. Redirects are configured in the fuel/application/config/redirects.php file. To add a redirect, add a URI location as a key and the redirect location as the value to either the passive_redirects (recommended) or aggressive_redirects config parameters. passive_redirects will only work if there are no pages detected by FUEL. This means that it will only incur the expense of looping through the redirects list if no pages are found. Alternatively, aggressive_redirects will redirect no matter what but will incur the cost of looping the the redirects list on every request.
You can also pass additional parameters such as the case_sensitive, http_code and max_redirects with your redirect if you use an array syntax like so:
// non key array $config['passive_redirects'] = array( 'news/my_old_news_item' => array('news/my_brand_new_news_item', TRUE, 302) ); // keyed array $config['passive_redirects'] = array( 'news/my_old_news_item' => array('url' => 'news/my_brand_new_news_item', 'case_sensitive' => TRUE, 'http_code' => 302) );
Key values can use regular expression syntax similar to routes.
Programmatically, you can access the instantiated Fuel_redirects object like so:
You can also set global configuration values from within the fuel/application/config/redirects.php file like so:
$config['http_code'] = 302; $config['case_sensitive'] = FALSE; $config['max_redirects'] = 2;
The max_redirects is used to prevent scenarios where it can cause an infinite redirect loop.
The ssl configuration provides an easy way to force SSL encryption (https) for your page URLs. The following example would restrict any url under "store" to use SSL encription when in the "production" environment:
$config['ssl'] = array('production' => array( '^store|store/:any' ));
The non_ssl configuration provides an easy way to force pages that are in SSL to not be encrypted (opposite of SSL). If there is an ssl configuration that conflicts with a non_ssl, then the ssl will take precedence and ignore the non_ssl to prevent infinte redirects. The following example would redirect any url under "about" to NOT use SSL encription when in the "production" environment:
$config['non_ssl'] = array('production' => array( '^about|about/:any' ));
The host configuration provides an alternative to .htaccess for redirecting your page if the current $_SERVER['HTTP_HOST'] PHP variable doesn't match what is specified as the "host" value in the redirect configuration. The following example would redirect any requests to "mysite.com" if the HTTP_HOST value doesn't match in the "production" environment (e.g. www.mysite.com):
$config['host'] = array('production' => 'mysite.com');
If you are needing to check the redirects, you can utilize Fuel_redirects::test method and create a simple view file at fuel/applications/views/redirects_test.php with the following:
<?php echo ''; print_r($CI->fuel->redirects->test()); echo ''; ?>
Regular expressions and shorthand :any will not be properly translated and will return errors.
- pre_redirect: Called right before a page is redirected
- pre_404: Called right before a 404 error page is displayed. You must use the redirect_404() function instead of the show_404() function for this hook to be executed.
Custom 404 Page
You can create a custom 404 page by either creating a page in the CMS or a view file named 404_error.
If you are having trouble with a redirect, check that you don't have any routes, or pages with assigned views at the URI path in question. Additionally, if you have the FUEL configuration setting $config['max_page_params'] with a value greater then 0, then you may run into issues where the rendered page is passing a segment as a parameter instead of redirecting. In that case, you may want to change the $config['max_page_params'] value to a key value pair with the keys being the URI paths and the values being the number of segments to accept at those URI paths (e.g. $config['max_page_params'] = array('about/news' => 1);)