Hitshot
Hitshot

Reputation: 31

How do I redirect HTTP to HTTPS?

I have this htaccess:

RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/my
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/my
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
DirectoryIndex index.php index.cgi index.html
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
#### PERSISTENT CONTENT ####
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ zyro/$1 [L,QSA]

and I want to know how to keep this code working and add HTTP-HTTPS redirection, any help is appreciated. Thank you

Upvotes: 2

Views: 1961

Answers (2)

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 2138

You can use the https protocol if you have a SSL certificate and have installed it correctly. Most websites use the http protocol as a default protocol to handle all the information. You can force your website to use the https protocol by creating or modifying an “.htaccess” file in the folder (e.g. root) where you want the redirect to happen.

Please add this to the .htaccess file

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Explanation

We are using three command in the code above: RewriteEngine, RewriteCond and RewriteRule. The “RewriteEngine On” tells Apache we are going to use mod_rewrite. The “RewriteCond %{HTTPS}” off, check if the the https protocol already in use. If the https protocol is use then the last line (RewriteRule) wont apply. The last line “RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}” tell the server to only rewrite the first part (http://) to (https://).

Upvotes: 0

Jack
Jack

Reputation: 1015

Try this. Add to your .htaccess

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Upvotes: 1

Related Questions