Filed under:

How To Use .HTACCESS to switch between HTTP and HTTPS




How To Use a .htaccess file Redirect to Force HTTPS instead of HTTP when you have an SSL Certificate

If you are not using CMS like WordPress which provides a configurable way to specify the URL you wih to use, a redirect can be implemented using the .htaccess file for web directives ...
Forcing visitors to use only the SSL version of your site can be accomplished using mod_rewrite rules in .htaccess

Redirect All Web Traffic

To force all web traffic to use HTTPS insert the following lines of code in the .htaccess file in your website(s) folder(s):
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.(yourdomain.ext)/$1 [R=301,L]
Replacing (yourdomain.ext)with your domain name (and optionally removing the www. part from the command)

Redirect Only Specified Folders

If you only want to force SSL on a specific folder or folders you can insert the following lines of code into a .htaccess file placed in that specific folder
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} (yourfolder)
RewriteRule ^(.*)$ https://www.(yourdomain.ext)/(yourfolder)/$1 [R=301,L]
Replacing (yourdomain.ext) with your domain name and (yourfolder) with your folder name

Note: to view/access/upload/download .htaccess files in your FTP client program you may need to enable "hidden files" otherwise by default it will not show things which start with a dot.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
article icon What is PHP? (Views: 2730)

Language: