Tom Chambers
Tom Chambers

Reputation: 374

.htaccess code to make http go to https with a CodeIgniter complication

I use codeigniter with some URL routing.

I want to migrate from http to https.

This is my current .htaccess code:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^eternalcitytours\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX [NC]
RewriteRule ^(.*)$ http://eternalcitytours.com/$1 [R=301,L]

<IfModule mod_rewrite.c>
# Development
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

The above works fine on http.

To go to https i changed the .htaccess to be:

RewriteEngine On 
  RewriteCond %{HTTPS} !=on 
  RewriteRule .* https://eternalcitytours.com/%{REQUEST_URI} [R,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} !^eternalcitytours\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^037\.061\.237\.216 [NC]
RewriteRule ^(.*)$ https://eternalcitytours.com/$1 [R=301,L]

<IfModule mod_rewrite.c>
# Development
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

However, the above code doesnt work as I get 2 problems:

  1. whenever incoming traffic comes to me from an http link, the resultant url has an extra / in it: e.g. https://eternalcitytours.com//en/5/About-Eternal-City-Tours instead of https://eternalcitytours.com/en/5/About-Eternal-City-Tours
  2. If someone has https://WWW.eternalcitytours.com then the page wont load and an error is given. (ERR_SSL_PROTOCOL_ERROR)

Can someone help with my .htaccess code as i honestly dont know what i'm doing.

thanks

Upvotes: 1

Views: 1271

Answers (1)

anubhava
anubhava

Reputation: 785146

You need to use this rule for http -> https:

RewriteCond %{HTTPS} !=on 
RewriteRule ^ https://eternalcitytours.com%{REQUEST_URI} [R=301,NE,L]

Note / removed before %{REQUEST_URI}.

Make sure to clear browser cache before testing it.

For problem #2 it seems your SSL cert is for eternalcitytours.com domain only. It is because SSL handshake hapens before mod_rewrite rules are executed and there is no rule based fix except that you keep https links with www in your HTML source.

Upvotes: 1

Related Questions