Before reading on, note that the .htaccess file is deceptively powerful. Making syntax errors on this file can result in your website becoming inaccessible. That said, as long as you have backed up your previous .htaccess file, you are generally safe to make adjustments, knowing that you can revert to your backed-up file if anything goes wrong. As such, please back up a safe copy of your existing .htaccess file before making any changes. Once you’ve done that, read on for a guide as to how you can force web requests to redirect to the https version of your website, while always displaying the “www” sub-domain as canonical in the website’s URL.

Creating an .htaccess file to redirect your website to HTTPS and append the www sub-domain can be a great way to improve your site’s security and SEO. Search engines consider non-www and www versions of the same website to be examples of duplicate content, which can create indexing issues. By showing both viewers and search engines that only the www version of your URLs should be accessed, this becomes your canonical address and can therefore help your search engine rankings. Also, forcing all requests to use https is both good for SEO, and helps to safeguard your users’ data as they interact with your website.

This tutorial is designed for website publishers of any experience level, so I’ll walk you through each step in detail.

Step-by-Step Tutorial for Creating an .htaccess File

Step 1: Access Your Website’s Root Directory

  1. Log into your web hosting account. This is usually done through a control panel like cPanel, Plesk, or a similar web hosting dashboard, but may also be done through FTP if you’re comfortable using that method.
  2. Navigate to the File Manager. This is where all your website files are stored.
  3. Locate your website’s root directory. Typically, this is named public_html or www.

Step 2: Check for an Existing .htaccess File

  1. Search for an existing .htaccess file in your root directory. It’s a hidden file, so you might need to enable ‘Show Hidden Files’ in your file manager settings.
  2. If the file exists, download a backup before making changes. This is vital! Do not make any changes until you have a safe backup ready to restore.
  3. If the file doesn’t exist, you’ll create a new one.

Step 3: Create or Edit the .htaccess File

  1. Right-click in the file manager and select ‘Create New File’ if you don’t have an existing .htaccess file. Name it .htaccess.
  2. Edit the file by right-clicking on it and selecting ‘Edit’ or ‘Code Edit’.

Step 4: Write the Redirect Code

  1. Copy and paste the following code into your .htaccess file:
    • RewriteEngine On activates the rewrite module.
    • RewriteCond lines set the conditions for the redirect: if HTTPS is off or the ‘www’ subdomain is missing.
    • RewriteRule defines the action taken when conditions are met: redirect to https://www. version of your site.
  2. Ensure no syntax errors. Mistakes in the .htaccess file can make your website inaccessible. Again, don’t panic about this – but make sure that you can restore a backup of the original file before making changes.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Step 5: Save and Upload the File

  1. Save the changes to the .htaccess file.
  2. If you created a new file locally, upload it to your website’s root directory using the file manager or an FTP client.

Step 6: Test Your Website

  1. Open your website in a web browser.
  2. Test different URLs (both with and without ‘www’, and with ‘http’ instead of ‘https’).
  3. Ensure they all redirect to the https://www. version of your site.

Troubleshooting

  • If your website is inaccessible, restore the backup of your .htaccess file.
  • Check for typos in the code.
  • Consult your hosting provider’s support if you’re unsure about any steps.

Conclusion

You’ve successfully set up an .htaccess file to redirect your website to HTTPS and ensure that the www sub-domain is used. This not only enhances your site’s security but also helps with search engine optimization.

Remember, it’s always good to have a backup of your website files before making any changes. If you encounter issues, don’t hesitate to reach out to your web hosting provider’s support team for assistance. Good luck!

Visited 262 times, 1 visit(s) today
Was this article helpful?
YesNo

Leave a Reply

Close Search Window