Reetika
Reetika

Reputation: 123

How do I enable https only on certain pages with htaccess also with removal of index.php from URL in Codeigniter?

I have used this code

Options +FollowSymLinks
RewriteEngine on

# redirect for http /buy page
RewriteCond %{SERVER_PORT} =80
RewriteRule ^buy/?$ https://mysite.com/buy [R=301,QSA,L,NE]

# redirect for https non /buy pages
RewriteCond %{SERVER_PORT} =443
RewriteCond %{REQUEST_URI} !^/buy [NC]
RewriteRule ^/?(.*)$ http://mysite.com/$1 [R=301,QSA,L,NE]

This is working fine for https redirection, but i also want to remove index.php from my URL.Here is the code for that:

RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

How can i join these two codes. Please Help.

Upvotes: 0

Views: 240

Answers (1)

Rooneyl
Rooneyl

Reputation: 7902

You could do it in code.

Create a helper function; something like

if ( ! function_exists('force_ssl')) {
    function force_ssl() {
        $CI =& get_instance();
        $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] != 443) {
            redirect($CI->uri->uri_string());
        }
    }
} 

Then just call this function in the construct of the controller you want.

E.g

public function __construct() {
        parent::__construct();
        force_ssl();
}

Upvotes: 2

Related Questions