Redirect non-www to www & http to https using mod_rewrite .htaccess

Posted
Comments 0

Learn the right way to redirect non-www to www and http to https using mod_rewrite and .htaccess

Today I’ll show you the right way to redirect non-www to www, and http to https using Apache mod_rewrite and an .htaccess file. There are a lot of guides on how to do this, but many of them are simply wrong and can crash the server causing a HTTP 500 Error.

The reason we want to redirect non-www to www is that Google and other search engines will index both versions, which is not ideal. We also want to redirect http to https to avoid those pesky browser warnings that users will simply close if they encounter them, losing you traffic.

Contents

Create or edit .htaccess file

You will likely already have an .htaccess file in your web root folder (public_html). If not, create the new file in your text editor (Jedit, VSCode etc.) or follow the directions below.

Windows

  1. On Windows, create a new .txt file, then save it as .htaccess. with a dot each side of the name (remove the .txt extension too).

macOS

  1. On macOS, open a new TextEdit file, then save it as .htaccess (you will get a warning, click on OK).

Set up mod_rewrite

Before we enter our directives, we need to create an IfModule bracket so that if mod_rewrite is not enabled on your server, it won’t throw a 500 error:


<IfModule mod_rewrite.c>
	#directives go here
</IfModule>

Please note that you may already have an IfModule mod_rewrite.c section in your .htaccess file (WordPress for instance). If you do, place the directives for this guide above everything else (see example further below).

Enter Directives

Now we enter our directives and I will explain each line afterwards (replace ricmedia.com with your domain name):


<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{HTTPS} off [OR]
	RewriteCond %{HTTP_HOST} ^ricmedia.com [NC]
	RewriteRule ^(.*)$ https://www.ricmedia.com/$1 [L,R=301]
</IfModule>

I will explain each rule in detail:

  1. RewriteEngine On This turns the mod_rewrite engine on
  2. RewriteCond %{HTTPS} off [OR] this line says if HTTPS is off, condition is triggered. The [OR] tag means this condition or the next
  3. RewriteCond %{HTTP_HOST} ^ricmedia.com [NC] This line means if ricmedia.com was requested without the www then condition is triggered
  4. RewriteRule ^(.*)$ https://www.ricmedia.com/$1 [L,R=301] this is the rewrite rule if either condition above is triggered. This rule redirects to the desired URL of https://www.ricmedia.com/ with a 301 flag (permanent redirect).

If you already have an IfModule mod_rewrite.c bracket and you’re running WordPress, place directives as follows:


<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{HTTPS} off [OR]
	RewriteCond %{HTTP_HOST} ^ricmedia.com [NC]
	RewriteRule ^(.*)$ https://www.ricmedia.com/$1 [L,R=301]
	#WordPress Directives
	RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
	RewriteBase /
	RewriteRule ^index\.php$ - [L]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . /index.php [L]
</IfModule>

Now you can upload the .htaccess file to your website’s root folder.

Conclusion

Your website should now be redirecting to www and https. If you had any issues, hit me up in the comments or socials/email.

See Also

Further Reading

Author
Categories Apache

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.