Vikram
Vikram

Reputation: 3351

Force redirect to https by htaccess

I have domain and a subdomain like example.com and blog.example.com and enabled ssl for both using wildcard on bluehost. I add following code suggested by bluehost

Custom subdomain .htaccess SSL + WordPress

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
    RewriteCond %{REQUEST_URI} !^/subfolder/
    RewriteRule ^(.*)$ /subfolder/$1
    RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
    RewriteRule ^(/)?$ subfolder/index.php [L]
    # End custom subdomain .htaccess

    # Custom maindomain .htaccess WordPress
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # End custom maindomain .htaccess

sites are running on https properly but they are accessible on http as well.

i tried following code to force redirect but its not working.

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

also i have two htaccess file for main domain and for subdomain and above code is placed in main domain htaccess file.

am i doing something wrong or missing something to force all urls to open with https:// ?

Upvotes: 0

Views: 381

Answers (1)

thepiyush13
thepiyush13

Reputation: 1331

Have you tried this :

For only domain

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

For subdomain(place .htaccess inside subdomain folder)

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://blog.example.com/$1 [R,L]

Upvotes: 1

Related Questions