Force users to use http://mysite.com

The following code will force all of your website users to use the non-www version of your site to access all of the content that is present in your site just copy and paste this code in your .htaccess file and change mysite.com name to your domain name:

# Redirect www urls to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mysite\.com [NC]
RewriteRule (.*) http://mysite.com/$1 [R=301,L]


Force users to use http://www.mysite.com

If you wish to redirect all of your website users to www version of your site to access all of the content of your site paste the following code in your .htaccess file and change mysite.com to your domain name:

# Redirect non-www urls to www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]


Force redirection to HTTPS

Below we will cover how to force redirection to HTTPS, this is useful if you have an SSL certificate installed and you want all your viewers to visit the https section of your site. You will need to have a file with the name of '.htaccess' in your public_html directory. Once this file is created you just need to copy the code that you will find below to the top of the file.

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


Force redirection to HTTPS and www

Below we will cover how to force redirection to HTTPS and www, this is useful if you have an SSL certificate installed and you want all your viewers to visit the https://www.yourdomain.com section of your site. You will need to have a file with the name of '.htaccess' in your public_html directory. Once this file is created you just need to copy the code that you will find below to the top of the file.

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Default Homepage

Sometimes we need to specify a custom index page, this can easily be implemented with the following code that needs to be placed in your .htaccess file. You will need to have a file with the name of '.htaccess' in your public_html directory. Once this file is created you just need to copy the code that you will find below to the top of the file. You may replace 'home.html' with the name of the page which you wish to be displayed when your visitors reach your website.

# Specify a default home page (index page)
DirectoryIndex home.html


Default home folder

There are times here you may wish to change the document root of your site to load from a sub-directory within your public_html folder, this can be achieved through htaccess. You will need to have a file with the name of '.htaccess' in your public_html directory. Once this file is created you just need to copy the code that you will find below to the top of the file.

# Set a default home directory, (this subfolder always loads)
# Replace 'folder' with your subfolder name

RewriteEngine On
RewriteRule ^$ /folder/ [R=301,L]


Did you find this article useful?