EN · $ USD
Web Hosting and cPanel

301 Redirects and Forcing HTTPS with .htaccess

7 min read Updated: Sunucu Atlas technical team

In short

To set up a 301 redirect with .htaccess, add mod_rewrite rules to the .htaccess file in your site's root directory. After the RewriteEngine On line, one rule can check for HTTPS, enforce your www or non-www preference and send old URLs to the new address with a single 301. To avoid loops, only redirect when a condition requires it, and test new rules with a 302 first.

In this guide
  1. What is a 301 redirect and why does it matter?
  2. Where is the .htaccess file and how do you edit it?
  3. Redirect HTTP to HTTPS (force HTTPS)
  4. www and non-www redirect templates
  5. 301 redirect from an old URL to a new URL
  6. All in one: sample .htaccess for WordPress
  7. How to prevent a redirect loop (ERR_TOO_MANY_REDIRECTS)
  8. Testing your redirects
  9. Next step: run your redirects on the right platform

A 301 redirect in .htaccess sends visitors and search engines from an old address to a new one permanently. Whether you are moving from HTTP to HTTPS, consolidating www and non-www, or pointing a moved page to its new URL, the fastest fix is a few lines of mod_rewrite rules in the .htaccess file in your site's root directory. Below are copy-paste templates and tips for avoiding the most common mistake: a redirect loop.

What is a 301 redirect and why does it matter?

301 Moved Permanently is the HTTP status code that says a resource has moved to a new address for good. Browsers go to the new address, and Google gradually passes the old page's ranking signals to the new URL. 302 Found is a temporary redirect; search engines keep the old address in their index.

Badly configured redirects cause three problems: the same content gets indexed at four addresses (http://, https://, www and non-www), creating duplicate content; visitors from old links land on a 404 page; and browsers show an ERR_TOO_MANY_REDIRECTS error.

Where is the .htaccess file and how do you edit it?

.htaccess is a per-directory configuration file read by Apache-compatible web servers. LiteSpeed Web Server also understands Apache's mod_rewrite syntax, so the rules below work unchanged on cPanel hosting plans running LiteSpeed Web Server.

  1. In cPanel, open File Manager and go to the public_html folder.
  2. In the Settings menu at the top right, check Show Hidden Files (dotfiles).
  3. If a .htaccess file exists, download a backup copy first; if not, create a new file with that name.
  4. Open the file with Edit, add the rules at the top (on WordPress, keep them out of the # BEGIN WordPress block and place them above it) and save.

A single typo can trigger a 500 error. If that happens, remove the lines you just added; see the 500 Internal Server Error troubleshooting guide for details.

Redirect HTTP to HTTPS (force HTTPS)

First make sure your domain has a valid SSL certificate. Forcing HTTPS without one sends visitors straight to a security warning. To install one, follow the free SSL with AutoSSL and Let's Encrypt guide.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

This rule only fires when the request is not HTTPS; that condition is what prevents a loop. Because %{HTTP_HOST} uses the incoming domain as is, the same file works for your main domain and any addon domains.

If you are behind a proxy or CDN

If a proxy or CDN in front of your site terminates HTTPS itself, requests may reach the server as HTTP and the rule above will loop forever. In that case, also check the X-Forwarded-Proto header sent by the proxy:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

www and non-www redirect templates

For SEO, it does not matter which one you choose; what matters is picking one version and permanently redirecting the other to it.

www to non-www

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

non-www to www

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

This rule also adds www. to every subdomain. If you use subdomains, limit the condition to your own domain: RewriteCond %{HTTP_HOST} ^alanadi\.com$ [NC].

Recommended: HTTPS and non-www in one step

Separate rules send http://www.alanadi.com first to HTTPS and then to non-www, which means two hops. The combined rule below reaches the target with a single 301 in every case:

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

If either of the first two conditions is true (HTTPS is off or the host starts with www), the third condition captures the domain without www and reuses it as %1. The NE flag stops special characters in the URL from being encoded twice.

301 redirect from an old URL to a new URL

Use the patterns below when you restructure your site, rename pages or move to a new domain. Place these rules below the HTTPS/www rules.

ScenarioRule
Single pageRewriteRule ^eski-sayfa\.html$ /yeni-sayfa/ [L,R=301]
Moving a folder (with subpages)RewriteRule ^blog/(.*)$ /makaleler/$1 [L,R=301]
Removing the extension (.php)RewriteRule ^(.+)\.php$ /$1 [L,R=301]
Old URL with query parametersRewriteCond %{QUERY_STRING} ^id=15$ + RewriteRule ^urun\.php$ /urun/kirmizi-kazak/? [L,R=301]

Only use the extension-removal rule if the server can also serve the extensionless URL (for example, when a framework router handles it); otherwise the new address returns a 404. In the query-string example, the trailing ? stops the old query string from being carried over to the new URL.

Changing your domain name

After you connect the old domain to the new hosting account as an addon or parked domain, redirect it while preserving every path:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?eskialanadi\.com$ [NC]
RewriteRule ^(.*)$ https://yenialanadi.com/$1 [L,R=301]

The old domain must stay active on the hosting account so it can keep getting SSL certificates and this rule also works for HTTPS requests. If your plan has no room for another domain, consider moving to the SA-CP-2 hosting plan, which includes addon and parked domain limits.

A simpler alternative with mod_alias

For a handful of individual pages, there is a more readable option:

Redirect 301 /eski-sayfa.html https://alanadi.com/yeni-sayfa/

Try not to mix Redirect and RewriteRule in the same file; the two modules run at different stages and can produce unexpected results. For consistency, it is safer to write every rule with mod_rewrite.

All in one: sample .htaccess for WordPress

Rule order directly affects the result. Fix the domain and protocol first, then handle page-level redirects, and let the application's own router run last. The example below shows the correct order for a WordPress site that prefers non-www and HTTPS:

# 1) Protocol and domain: https + non-www in one step
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

# 2) Moved pages
RewriteRule ^hakkimizda\.html$ /hakkimizda/ [L,R=301]
RewriteRule ^blog/(.*)$ /makaleler/$1 [L,R=301]

# BEGIN WordPress
# (Do not touch the block WordPress adds itself)
# END WordPress

Each time you save the permalink settings, WordPress rewrites only the section between # BEGIN WordPress and # END WordPress. Keep your own rules outside that block and they will survive updates.

How to prevent a redirect loop (ERR_TOO_MANY_REDIRECTS)

A loop happens when address A redirects to B and B redirects back to A. The most common causes are:

  • Unconditional rule: An HTTPS rule written without RewriteCond %{HTTPS} off redirects requests that are already HTTPS.
  • Proxy/CDN conflict: If the CDN connects to your server over HTTP while you force HTTPS, you get a loop. Switch the CDN's SSL mode to end-to-end encryption or add the X-Forwarded-Proto check.
  • Application conflict: If the site address under Settings > General in WordPress includes www but .htaccess strips www, the two keep redirecting each other. The application's address setting and your .htaccess preference must match.
  • Duplicate plugins: If an SSL plugin and .htaccess do the same job, turn one of them off.

Testing your redirects

Browsers cache 301 responses, so even after you fix a bad rule, your browser may keep following the old redirect. That is why you should try new rules with R=302 first and switch to R=301 once everything is correct. Test in a private window or from the command line:

curl -I http://www.alanadi.com/eski-sayfa.html

The response should show HTTP/1.1 301 Moved Permanently and the correct Location: header. Use curl -IL to follow the chain to the end; ideally the target is reached in no more than one hop. For the full list of flags, see the Apache mod_rewrite documentation.

Next step: run your redirects on the right platform

For redirect rules to work smoothly, the server needs .htaccess support, a valid SSL certificate and, when needed, room to host extra domains. Sunucu Atlas cPanel web hosting plans come with LiteSpeed Web Server and a free SSL certificate, so you can add the templates above straight to your public_html/.htaccess file. If you need to keep the old and new domains on the same account, check the Site Limit (Addon Domain) and Park Limit rows in the plan comparison. If a rule gives you trouble, open a support ticket and ask for help (support is provided in Turkish).

Frequently asked questions

What is the difference between a 301 and a 302 redirect?

A 301 is a permanent redirect, and search engines pass the old address's signals to the new one. A 302 is temporary; the old address stays in the index.

I added an .htaccess rule and my site shows a 500 error. What should I do?

There is most likely a typo. Delete the lines you just added or restore your backup .htaccess file, then add the rules back one at a time to find the faulty line.

Why do I get ERR_TOO_MANY_REDIRECTS after forcing HTTPS?

Usually the rule has no condition, or a CDN in front of your site connects to the server over HTTP. Add the RewriteCond %{HTTPS} off condition and, if needed, the X-Forwarded-Proto check, and set the CDN's SSL mode to end-to-end encryption.

Is www or non-www better for SEO?

Search engines treat them the same. What matters is choosing one version, 301 redirecting the other to it and keeping your application's site address setting consistent.

Do .htaccess redirects work on a LiteSpeed server?

Yes. LiteSpeed Web Server reads Apache's mod_rewrite syntax, so the rules in this guide work without changes.

I fixed the redirect, but my browser still goes to the old address.

Browsers cache 301 responses. Test in a private window or with the curl -I command; trying new rules with a 302 first prevents this problem.

For this guide

Recommended plans

cPanel hosting plans with LiteSpeed Web Server and a free SSL certificate run the .htaccess redirect and force-HTTPS rules in this guide with no extra setup.

SA-CP-1

cPanel
Recommended

20 GB NVMe disk

$0.39/mo

+VAT

  • Memory1 GB
  • LocationIstanbul
  • 1 Hosted Domains
  • 50 GB Traffic
  • 5 Email Account Limit
  • 2 Databases

SA-CP-2

cPanel

40 GB NVMe disk

$0.69/mo

+VAT

  • Processor2 cores
  • Memory2 GB
  • LocationIstanbul
  • 1 Hosted Domains
  • 100 GB Traffic
  • 10 Email Account Limit
  • 5 Databases

SA-CP-3

cPanel

60 GB NVMe disk

$0.98/mo

+VAT

  • Processor3 cores
  • Memory3 GB
  • LocationIstanbul
  • 1 Hosted Domains
  • 200 GB Traffic
  • 25 Email Account Limit
  • 10 Databases

Web Hosting — all plans

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.