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
- Set up mod_rewrite
- Enter Directives
- Conclusion
- See Also
- Further Reading
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
- 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
- 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:
RewriteEngine On
This turns themod_rewrite
engine onRewriteCond %{HTTPS} off [OR]
this line says if HTTPS is off, condition is triggered. The[OR]
tag means this condition or the nextRewriteCond %{HTTP_HOST} ^ricmedia.com [NC]
This line means if ricmedia.com was requested without thewww
then condition is triggeredRewriteRule ^(.*)$ 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 ofhttps://www.ricmedia.com/
with a301
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
- The Ultimate Web Server on Ubuntu 22.04
- Initial Server Setup with Ubuntu 22.04
- Create an Apache Virtual Host on Ubuntu 22.04
Comments
There are currently no comments on this article.
Comment