To force HTTPS with .htaccess on Apache, enable the rewrite engine, check whether the request is using HTTP, and redirect it to the corresponding HTTPS URL. The HTTPS version must already work with a valid certificate before you enable the redirect.
We explain a root-file example below, including verification and rollback. If your site sits behind Cloudflare or another reverse proxy, read the proxy section first: Apache's connection to that proxy may not use the same scheme as the visitor's connection.
Step 1: make sure HTTPS works before redirecting
Open your intended HTTPS hostname directly and confirm that it loads without a certificate warning. Check the hostname visitors will actually use, including whether your chosen address includes www. A redirect cannot install a certificate or fix a mismatch between a certificate and the requested name.
HTTPS protects the connection using TLS. It does not prove a site's claims are true or make the application immune to security problems. We focus here on getting visitors onto the secure URL rather than making promises about trust, conversions or rankings.
If the secure version fails, stop and resolve the certificate or hosting issue first. Ask your host which certificate and server setup the domain requires. Our web hosting guide explains why different hosting arrangements offer different levels of configuration access.
Need the domain first?
Runs the same live check as the front page. If it is taken we will show you when it expires.
Step 2: check whether a redirect already exists
Request an HTTP URL and inspect its first response before adding any code. Your host or CDN may already redirect it. For example, DreamHost documents automatic HTTPS redirection after certificate setup for its supported configurations. Adding another rule without checking can create conflicting behavior.
Choose which layer owns the redirect and keep that decision clear. A rule at the hosting platform, a Cloudflare edge redirect and an application setting are not necessarily independent. They all influence the request path a visitor experiences.
If you use Cloudflare, our Cloudflare HTTPS redirect guide covers that alternative. Do not enable several approaches simply because they all mention HTTPS.

Step 3: back up the site's .htaccess file
These instructions apply to an Apache setup where mod_rewrite directives are permitted in .htaccess. They are not Nginx configuration. Locate the document root assigned to this website, show hidden files if needed, and download a copy of the existing file.
Edit the existing file rather than replacing the application's configuration. On WordPress, place custom rules outside the managed # BEGIN WordPress and # END WordPress block so WordPress does not overwrite them. Putting the HTTPS redirect before application routing also makes its purpose easier to inspect.
Our .htaccess overview explains directory-level configuration. If you administer the full Apache configuration, consider a dedicated HTTP virtual host redirect instead; .htaccess is primarily useful when you do not have that access.
Step 4: add a conditional HTTPS rule
For a site where Apache itself receives the visitor's HTTP or HTTPS connection, use this root-file example. Replace example.com with the single canonical hostname you have verified:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]Using an explicit hostname makes the destination clear. Do not paste the example unchanged on a real site, and do not use it for multiple unrelated domains sharing a document root unless sending them all to that hostname is intentional.
RewriteEngine Onenables rewriting in this context; it does not install a missing module.RewriteCond %{HTTPS} !=onlimits the following rule to requests Apache does not recognize as HTTPS.%{REQUEST_URI}carries the requested path to the destination.R=301returns a permanent redirect;Lstops the current rewrite pass.
The rule contains no replacement query string, so ordinary query parameters are retained. Test the URLs your application actually uses, especially paths containing encoded characters, rather than assuming every unusual path behaves identically.
For an initial check, you can use R=302 while you verify the destination, then change it to R=301 when the move is permanent. Avoid testing uncertain rules with permanent redirects that clients may cache.
Step 5: test HTTP, HTTPS and important paths
- Save the file and request an HTTP page, not just the homepage.
- Confirm the response redirects to the intended HTTPS hostname and path.
- Follow the response and check that the destination loads.
- Request that HTTPS URL directly and confirm it does not redirect to itself.
- Test a URL with a query string and an important application route.
- Check any alternate hostname you intend to support.
Browser network tools show the response chain. From a terminal, curl -I http://example.com/contact/ inspects the first response, and curl -IL http://example.com/contact/ follows redirects. Inspect status and Location values instead of relying only on the address bar.
If you also want all HTTPS www requests to use the non-www hostname, the example above does not fully implement that policy: an already-secure request fails the HTTP condition. Treat hostname canonicalization as a separate requirement and test both schemes. Our 301 redirect examples explain the mapping basics.
Cloudflare and reverse proxies: avoid redirect loops
Cloudflare's Flexible encryption mode sends requests to the origin over HTTP. If the origin always redirects that connection to HTTPS, the visitor can become stuck in a loop even though their browser is already using HTTPS.
Cloudflare documents resolving that conflict by removing origin HTTPS redirects or using Full or a higher mode with a suitable origin certificate. Choose a configuration that matches your certificate setup; do not simply suppress the warning without understanding which connection is encrypted.
Other reverse proxies may terminate TLS before forwarding to Apache. Their trusted scheme information needs a provider-specific configuration. Do not blindly copy a rule based on X-Forwarded-Proto from another host: confirm how your provider sets and validates that header.
Common questions
What if the site shows an error after saving?
Restore your backup, then inspect the Apache error log or ask your host to identify the rejected directive. If the problem is a loop rather than a server error, compare the destination at each step and check for opposing HTTP and HTTPS rules.
Does the rule create a certificate or check DNS?
No. It changes HTTP responses on the server receiving requests. Our domain lookup tool can help review registration information, but certificate and redirect checks must be performed against the website itself.