How to Redirect HTTP to HTTPS and www Using .htaccess
To redirect HTTP to HTTPS, install an SSL certificate, then add a 301 rule to the .htaccess file in your site root. Our .htaccess Redirect Generator creates rules that force HTTPS and your chosen www or non-www version in a single redirect – just enter your domain and copy the code.

Key facts
- Requires: an active SSL certificate and an Apache or LiteSpeed server
- Uses a permanent 301 redirect
- Generator forces HTTPS + www or non-www in one hop
- Place the rules at the top of .htaccess in your site root
Why one HTTPS version matters
✓🛠️ Without a redirect, your site can open at four addresses: http://, https://, with and without www. Search engines may treat them as duplicates and split your ranking signals, and visitors on http:// see a "Not secure" warning. One 301 redirect to a single https address fixes both.
Generate your rules – step by step

Step 1: Enter your domain
Open the .htaccess Redirect Generator and type your domain, for example example.com.
Step 2: Choose www or non-www
Pick "www to non-www" or "non-www to www". Both options also force HTTPS.
Step 3: Copy the code
Copy the generated rules or save them as a file.
Step 4: Add them to .htaccess
Paste the rules at the top of the .htaccess file in your site root, save, and test.
The rules explained
✓Prefer non-www (https://example.com):
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Prefer www (https://www.example.com):
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The first condition catches any http:// request; the second catches the domain version you do not want. Either one sends the visitor to the correct https address with a single 301 – no redirect chains.
Test the redirect
- Open http://example.com, http://www.example.com and the https version you do not use – all should land on your chosen https address.
- Check the response with Get HTTP Headers: you should see one 301 and then 200.
- Confirm the certificate with the SSL Checker.
- Update your sitemap, canonical tags and Search Console property to the https version.
Avoid these mistakes
- Adding the rules before SSL is active – visitors get a certificate error.
- Mixing rules from different tutorials, which can create redirect loops.
- Leaving http:// image and script links in your pages – they cause "mixed content" warnings.
- Using a 302 (temporary) redirect – use 301 for a permanent move.
Open the Htaccess Redirect on pdfseotools.com – free, no sign-up and it works on any device.
Frequently Asked Questions
Do I need an SSL certificate first?
Yes. Install SSL in your hosting panel before redirecting – Hostinger, most hosts and Cloudflare offer free certificates. Redirecting without one shows a security error.
Should I choose www or non-www?
Either works for SEO. Pick one, redirect the other to it, and use the same version in Search Console, your sitemap and canonical tags.
Why use a 301 redirect?
A 301 tells search engines the move is permanent, so ranking signals pass to the new https address.
I get "too many redirects". What now?
This usually happens behind a proxy such as Cloudflare in "Flexible" SSL mode. Set Cloudflare SSL to "Full", or replace the HTTPS condition with RewriteCond %{HTTP:X-Forwarded-Proto} !https.
Where is the .htaccess file?
In your website root (often public_html). It may be hidden – enable "show hidden files" in your file manager.
Does this work on Nginx?
No. .htaccess works on Apache and LiteSpeed servers. Nginx uses a server block redirect instead.
Summary
SSL first, then one 301 rule that forces HTTPS and your preferred domain version, then test. Generate the code with the .htaccess Redirect Generator and check your site with more free webmaster tools.
Share on Social Media: