Redirect HTTP requests to HTTPS and ensure all requests to the WWW version of your website are redirected to the non-WWW version. This guide shows you how to update the .htaccess
file on your Apache web server, commonly used in LAMP (Linux, Apache, MySQL/MariaDB, PHP) stack setups.
Table of Contents
- Why Redirect HTTP to HTTPS and WWW to Non-WWW?
- Step-by-Step Guide to Redirect HTTP to HTTPS and WWW to Non-WWW
- Conclusion
- References
Why Redirect HTTP to HTTPS and WWW to Non-WWW?
Setting up proper redirects for your website is essential for security, SEO, and user experience. By forcing HTTPS, you ensure encrypted connections, which protect user data and improve search engine rankings. Redirecting www to non-www (or vice versa) prevents duplicate content issues and ensures consistency in your domain structure.
In this guide, we will configure your Apache server’s .htaccess
file to:
- Redirect all HTTP requests to HTTPS.
- Redirect all
www.
requests to the non-www version of your domain. - Ensure both rules work together without causing redirect loops.
Step-by-Step Guide to Redirect HTTP to HTTPS and WWW to Non-WWW
The following steps show how to perform the redirects already described in this article.
1. Locate Your .htaccess File
The .htaccess
file is typically found in the root directory of your website (public_html
or /var/www/html
). If it doesn’t exist, create a new file named .htaccess
. If this file already exists on your website, it is best to download it now and save a copy of it.
Remember that a file that starts with a dot (.
) will be hidden by default on a Linux system. If you save this file to a Linux system, you may need to change a setting in your file manager to see it (often Ctrl + H
will show hidden files, but this is system dependent). Consider renaming the backup version of this file saved on your computer, for example, to htaccess_original
.
2. Add the Redirect Rules
Copy and paste the following code into your .htaccess
file:
RewriteEngine on
RewriteBase /
# Redirect www to non-www (HTTP or HTTPS)
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Redirect HTTP to HTTPS (non-www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
3. How the .htaccess Redirect Rules Work for HTTPS and WWW
Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
→ Checks if the requested domain starts withwww.
.RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
→ Redirectswww.example.com
tohttps://example.com
while preserving the path.
Redirect HTTP to HTTPS (only for non-www requests)
RewriteCond %{HTTPS} off
→ Checks if the request is using HTTP.RewriteCond %{HTTP_HOST} !^www\. [NC]
→ Ensures the rule applies only to non-www requests.RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
→ Redirectshttp://example.com
tohttps://example.com
.
4. How These Rules Work Together
This configuration ensures:
http://example.com
→ redirects tohttps://example.com
http://www.example.com
→ redirects tohttps://example.com
https://www.example.com
→ redirects tohttps://example.com
https://example.com
→ remains unchanged
5. Test Your Redirects
After saving your .htaccess
file, test your redirects by visiting:
http://yourdomain.com
http://www.yourdomain.com
https://www.yourdomain.com
Ensure all requests are redirected correctly to https://yourdomain.com
6. Additional Tips
- Ensure your SSL certificate is properly installed to prevent HTTPS errors.
- Use a staging environment to test before deploying changes to a live site.
- Use online redirect checker tools to confirm there are no redirect loops.
Conclusion
Implementing these .htaccess
rules ensures that your website is always accessed securely via HTTPS and maintains a consistent domain structure without www
. This not only improves SEO rankings but also enhances user experience and security.
By following this guide, you can confidently set up these redirects on your LAMP-based website. If you encounter any issues, check your Apache configuration and error logs for troubleshooting.