Redirect HTTP to HTTPS and www or non-www with .htaccess

Print
  • 1

To make every visitor land on one secure address, put a few rewrite rules at the top of the .htaccess file in your site's folder. Below you will find ready rules for the usual cases: HTTP to HTTPS, dropping or adding www, and both at once in a single redirect.

Before you edit the file

  • The SSL certificate has to be active for both domain.md and www.domain.md. Otherwise the redirect sends visitors straight to a security warning. You can check it in cPanel under Security, then SSL/TLS Status (see the free AutoSSL certificate).
  • Open cPanel, then File Manager, and go to the folder of the domain: public_html for the main domain, or the folder you picked when adding an addon domain.
  • Names that start with a dot are hidden by default. Click Settings in the top right corner of File Manager and tick Show Hidden Files (dotfiles).
  • Download the current .htaccess to your computer, so you can put it back if something goes wrong. If the file is missing, create it with + File.

To change the file, right-click it and choose Edit. Insert the redirect at the very beginning, above blocks such as # BEGIN WordPress, then click Save Changes.

Ready-made rules

Pick one variant only and replace domain.md with your own domain. Several variants pasted one under another will conflict.

Only HTTP to HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

From www to the bare domain

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

The target already starts with https://, so a visitor of www also ends up on the secure address.

From the bare domain to www

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.md$ [NC]
RewriteRule ^ https://www.domain.md%{REQUEST_URI} [L,R=301]

HTTPS without www, in one step (recommended)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.md$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.md%{REQUEST_URI} [L,R=301]

The first condition limits the rule to your domain, so subdomains whose folders sit inside public_html are left alone. Any request over HTTP, or any request for www, goes to https://domain.md with one 301 instead of a chain of two.

HTTPS with www, in one step

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.md$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.md%{REQUEST_URI} [L,R=301]

HTTPS without touching files

If all you need is HTTP to HTTPS, cPanel has a switch for it. Open the Domains section, then Domains, and turn on Force HTTPS Redirect on the row of your domain. The switch can be used only after the domain has a valid certificate. To send visitors to a different domain or page, use the Redirects tool in cPanel.

How to test the redirect

  • Open a private browser window and try http://domain.md, http://www.domain.md and https://www.domain.md. Each of them should finish on the address you chose.
  • Browsers keep 301 redirects in memory for a long time. While you experiment, write R=302 instead of R=301, and switch back to 301 once everything works.
  • From a command line, curl -I http://domain.md should answer with code 301 and a Location line holding the new address.

Problems and fixes

The browser reports ERR_TOO_MANY_REDIRECTS

The address keeps bouncing between two versions. The usual causes:

  • The domain goes through Cloudflare with SSL mode "Flexible": the server always sees plain HTTP and redirects again. Set the mode to "Full (strict)", as explained in the Cloudflare guide.
  • In WordPress, Settings, then General, the two site addresses point to the other variant (for example with www while the rule removes it). Make them match the rule.
  • A redirect plugin, a rule created in cPanel Redirects and your own rule pull in different directions. Keep only one of them.

Error 500 right after saving

The file has a typing error, often a missing space or a broken character pasted from a word processor. Upload the copy you saved, then add the rule again carefully. More causes of this error are listed in the article on errors 500 and 503.

Nothing happens

Check that you edited the .htaccess of the right folder (an addon domain has its own), that the rule sits above the CMS block, and that you are not seeing an old redirect saved by the browser. Still stuck? Open a ticket and include the domain and the rule you used.

Related articles

Was this answer helpful?

Need help?

Write to us in the chat, by e-mail or open a ticket from your account.

Live chatOpen the chat Call us+373 795-800-80 Write to ussupport@tophost.md
Copyright © 2026 TopHost.MD. All Rights Reserved.