How to Redirect HTTP to HTTPS using htaccess

Apache web server

To redirect a website from HTTP to HTTPS, you can use the following code in .htaccess file RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] If you want to redirect HTTP to HTTPS and keep SSL verification pages on HTTP, this is needed on Cpanel servers. RewriteEngine on RewriteCond %{HTTPS} !=on [NC] RewriteCond %{REQUEST_URI} … Read more

Redirect Subdomain to Subdirectory

To redirect a subdomain to a subdirectory, use following .htaccess code. RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA] Redirect Subdirectory to Subdomain RewriteEngine on RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ RewriteRule ^/?subdomain/(.*)$ https://subdomain.example.com/$1 [R=301] See htaccess

React/Angular Application showing 404 error on refresh

On Apache server, React Application works fine, but if you refresh a page, it shows 404 error. This is because the application use BrowserRouter. To fix the error, create a .htaccess file with following content or following code, this results in http 200 for non-existant pages. Put this on the folder where your application index.html … Read more

Redirect a folder to another

To redirect a folder to another using .htaccess, create RedirectMatch 301 ^/OLD_FOLDER/(.*)$ /NEW_FOLDER/$1 Or RewriteEngine On RewriteRule ^OLD_FOLDER/(.*)$ /NEW_FOLDER/$1 [R=301,NC,L] Or Redirect 301 /OLD_FOLDER /NEW_FOLDER if new folder is on another domain, you can use https://new-domain.com/OLD_FOLDER See Redirect

Password Protect Site using htaccess

To password protect a web site or a sub folder using .htaccess, create a .htaccess file in the folder. vi .htaccess Add following content AuthType Basic AuthName “Restricted Content” AuthUserFile /etc/apache2/site-logins Require valid-user In this case, i used /etc/apache2/site-logins as AuthUserFile, this will store all user and password. You can change this file path to … Read more

Redirect site to HTTPS excluding a folder

On a web site, customer need to redirect all pages to HTTPS, but want to keep files in one of the folder on HTTP. For this, i used following in .htaccess file. RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} !^/auth/.* RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] Here any url like yourdomain.extn/auth/ will not get redirected to HTTPS. … Read more

Disable PHP on a folder

A web site had vlunerability, all allowed hacker to upload backdoor script to “uploads” folder used by the script. As a quick fix, i disabled PHP execution from “uploads” folder. Doing this for any site is a good dea when if your site is not vlunerable at the moment. Method 1 To disable PHP execution, … Read more

htaccess

Limit Access Using htaccess Apache Limit access to a url Disable PHP on a folder How to protect .git folder using htaccess Password Protect Site using htaccess React Application showing 404 error on refresh Redirect Subdomain to Subdirectory How to Create Custom 404 Error Page in Apache CPanel SSL Renew on password protected site Magento … Read more