Martin Hunt
Martin Hunt

Reputation: 1155

.htaccess force https on certain urls otherwise force http

I'm trying to force SSL on 3 pages of a website, and force the rest to http

mydomain.com/register , mydomain.com/checkout and mydomain.com/thanks all need to redirect to https: but any other page should just use http:

Is this possible?

I currently have some codeigniter specific stuff in my htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

Upvotes: 1

Views: 938

Answers (2)

TechCare99
TechCare99

Reputation: 1831

I think this code will work (specially for CodIgniter)

RewriteEngine on

# force HTTPS
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} (register|checkout|thanks)
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force HTTP
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !(register|checkout|thanks|css|img|js)
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L] 

Upvotes: 2

SenorAmor
SenorAmor

Reputation: 3345

Not a .htaccess solution, but here's how I force https connections:

// Redirect to secure port
if ($_SERVER['SERVER_PORT'] != 443)
{
    $location = 'Location: https://www.blah.com';
    header($location);
}

Hope this helps.

Upvotes: 2

Related Questions