.htaccess redirect to https
.htaccess is an important configuration file when dealing with web pages like .html, .php .asp etc. It’s used on the webserver running Apache that helps run, enable or disable many functionalities and features.
Today I want to share with you how you can use the .htacess file to redirect HTTP to HTTPS via .htaccess .
Before rewriting Redirecting something or rewriting be sure Rewrite Engine is on, it’s usually on by default but just double-check to be on the safer side and also make sure rewrite module is existing in your web server. Secondly, also note that this redirect only works if you have installed an SSL on the website.https://infinitydomainhosting.com/index.php?rp=/knowledgebase
If a user wants to access your website content via HTTP we can control forcefully redirect them to the HTTPS version of the website now go to this .htaccess file and first check RewriteCondRewrite condition HTTPS is not on positive HTTP is not on, then redirect them so that if anything comes via HTTP it will be forcefully redirected to the HTTPS.https://www.freecodecamp.org/news/how-to-redirect-http-to-https-using-htaccess/
We can simply achieve this by rewriting .htaccess.
How to create a .htacess file.
(a) Live Server (web host)
Head to the control panel and open file manager go to the public HTML and create a file called .htaccess
(b) Localhost
If you’re using localhost say, xampp you can go to the htdocs and create a file called “.htaccess” without the quotes. Remember it started with dot .htaccess and by default file started with the dot is hidden so you can go to the setting and check this checkbox show hidden files now Edit this file and type the following lines of codes to redirect all traffic from HTTP to HTTPS;
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
NB: Replace yourdomain.com with your matching domain name
Save and exit.
If you are running the same htaccess on localhost be sure to check this condition if HTTP host is not localhost because we cannot install SSL in localhost otherwise, it will cause internal server error.
Other related .htacess scripts
htaccess redirect 301 | htaccess force https | htaccess for wordpress | .htaccess redirect to https