Friday, September 27, 2013

codeigniter url rewrite index.php redirect to base url

.htaccess

RewriteEngine On

# Keep these lines even in maintenance mode, to have an access to the website
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(application|modules|plugins|system|themes) index.php/$1 [L]

call this php in codeigniter.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class HO_INC {

function __construct()
{
$this->config =& load_class('Config', 'core');
log_message('debug', "CORE Class Initialized");
}

public function index_php_to_base_url($method = 'refresh')
{
if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
{
return;
}

$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
{
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));

            if ( ! preg_match('#^https?://#i', $uri))
            {
                $uri = $this->config->site_url($uri);
            }

            switch($method)
            {
                case 'refresh' : header("Refresh:0;url=".$uri);
                    break;
                default : header("Location: ".$uri, TRUE, 200);
                    break;
            }
            exit;
        }
    }
}

No comments: