Redirect a website to https using .htacess file

Due to certain reason you minght need to run your website on SSL only. If you want to do this by making changes in your .htaccess file then below are the code snippen you need to add to your .htaccess file. Below code snippet redirects your website from non HTTP to HTTPS based on server port. Port 443 is being used for secure server.

RewriteEngine On

 




RewriteCond %{SERVER_PORT} !=443

RewriteRule ^(.*)$ https://yourdomain.com/$1 [R=301,L]

If you are running your website under a particular folder and want to do the same under the particular folder only then. You need to write below code under your .htaccess file

RewriteEngine On

RewriteCond %{SERVER_PORT} !=443

RewriteCond %{REQUEST_URI} yourfoldername

RewriteRule ^(.*)$ https://www.yourrdomain.com/yourfoldername/$1 [R=301,L]

Now the .htaccess file with above code needs to be put under root directory and it will check if there is any request coming for particualar folder only it will automatically redirects it to https based url.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...