# Redirects with .htaccess: www, HTTPS and 301 page redirects (cPanel)

https://www.gyrohosting.com/en/knowledgebase/article/htaccess-redirect-www-https-301

- **What you will learn**: How to force your site to HTTPS with one switch, redirect pages and domains with the cPanel **Redirects** tool, merge the www and non-www addresses with an `.htaccess` rule, and check that the redirect works.
- **Time**: 12 minutes

## 1. What is a redirect for?

A redirect automatically sends visitors and search engines that arrive at one address to another address. You usually need one in three cases:

- **Forcing HTTPS:** everyone who arrives with `http://` goes to the secure `https://` address.
- **One address (merging www):** `www.ornekfirma.com` and `ornekfirma.com` don't show the same site at two addresses; search engines see a single address.
- **Moving a page or a domain:** the old links and search ranking of a page whose address changed are not lost.

**301** is a permanent redirect, **302** a temporary one. Use 301 for permanent moves; search engines pass the value of the old address on to the new one.

![The right redirect type (301 or 302) and the easiest tool for what you want to do.](https://www.gyrohosting.com/resources/uploads/bilgi-merkezi/B44-01-en.webp)

## 2. Before you start

- Back up your `.htaccess` file: in File Manager, select the file and duplicate it with **Copy** as `.htaccess-backup`. Because the file is hidden, first turn on **Settings** → **Show Hidden Files (dotfiles)** (see **B41**, Section 3.3).
- Before redirecting to HTTPS, check that the site opens without errors over `https://`. The free SSL certificate is installed automatically once your domain points to the server.

> **Warning**
> 
> One wrong `.htaccess` line can take the whole site down with a 500 error. Test the site right after every change; if something breaks, put the backup back.

## 3. Force the whole site to HTTPS (one switch)

1. In cPanel, open **Domains** in the **Domains** section.
2. On your domain's row, turn the **Force HTTPS Redirect** switch on.
3. After a few seconds, open `http://ornekfirma.com`; the browser should switch to the `https://` address by itself.

![Domains → Domains: turn on the Force HTTPS Redirect switch on your domain's row and the whole site redirects to HTTPS.](https://www.gyrohosting.com/resources/uploads/bilgi-merkezi/B44-02-en.webp)

> **Info**
> 
> The switch may not turn on, or the browser may show a security warning, while the domain has no valid SSL certificate. If the certificate was just installed, wait a little and try again.

## 4. Redirect a page or domain: the Redirects tool

1. Open **Redirects** in the **Domains** section.
2. In the **Type** list, choose **Permanent (301)** or **Temporary (302)**.
3. In the **https?://(www.)?** list, choose your domain. If you want the same redirect for all your domains, choose **All Public Domains**.
4. In the box next to the domain, type the old path, for example `eski-urunler` (don't type a leading `/`; the tool adds it). Leave the box empty to redirect the whole domain.
5. In **Redirects to**, type the full new address, for example `https://ornekfirma.com/urunler/`.
6. Under **www. redirection**, **Redirect with or without www.** is usually the right choice.
7. To send every page of an old domain to the same page on the new domain, tick **Wild Card Redirect**.
8. Press **Add**.

![Domains → Redirects: enter the type, the domain, the old path and the new address, then click Add.](https://www.gyrohosting.com/resources/uploads/bilgi-merkezi/B44-03-en.webp)

The redirect you added appears in the **Current Redirects** table at the bottom of the page. To remove it, click **Delete** on its row.

![The new redirect appears in the Current Redirects table; to remove it, use Delete in the Actions column.](https://www.gyrohosting.com/resources/uploads/bilgi-merkezi/B44-04-en.webp)

### If you use WordPress: move the lines up

The tool adds the rule at the **end** of `public_html/.htaccess`. Because WordPress's `# BEGIN WordPress` block runs before rules that come after it, the redirect never fires and the old address returns 404. We saw exactly this on our test account.

The fix:

1. In File Manager, select `public_html/.htaccess` and click **Edit**.
2. Cut the `RewriteCond` and `RewriteRule` lines the tool added and paste them **above** the `# BEGIN WordPress` line. Add `RewriteEngine On` once, directly above them.
3. Press **Save Changes** and try the old address again.

![If you use WordPress, redirect lines must sit above the # BEGIN WordPress block; if the tool adds them at the end of the file, move them up.](https://www.gyrohosting.com/resources/uploads/bilgi-merkezi/B44-05-en.webp)

> **Warning**
> 
> Don't delete or change the block that starts with `# php -- BEGIN cPanel-generated handler, do not edit`; it sets your site's PHP version.

## 5. www and HTTPS in one go: ready-made .htaccess rules

cPanel has no separate switch for redirecting the www address to the non-www address; you do it with an `.htaccess` rule.

1. In File Manager, open `public_html/.htaccess` and click **Edit** (if the file doesn't exist, create it named `.htaccess` with **+ File**).
2. Paste the rules below at the **top** of the file, before the `# BEGIN WordPress` line.
3. Press **Save Changes**.

```
RewriteEngine On

# 1) Send www requests to the non-www HTTPS address
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

# 2) Send HTTP requests to the HTTPS address
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# 3) Move a single page permanently to its new address
Redirect 301 /old-page.html /new-page/
```

![An example that combines www merging, HTTPS and a single-page redirect in one .htaccess; each rule has a comment line above it.](https://www.gyrohosting.com/resources/uploads/bilgi-merkezi/B44-06-en.webp)

These rules don't name the domain, so they work unchanged on any domain. On our test account we tried all four cases: the `http://`, `http://www.` and `https://www.` addresses went to the non-www `https://` address in a single step, and the old page went to the new page with a 301.

If you want the opposite (redirect the non-www address to www), use this instead of rule 1:

```
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
```

> **Info**
> 
> If you turned on the **Force HTTPS Redirect** switch on the Domains screen, rule 2 isn't needed; using both does no harm but is redundant.

## 6. Check that the redirect works

1. Open a private window in your browser (so the cache doesn't remember an old redirect).
2. Type the old address and check that the new address appears in the address bar.
3. Try `http://`, `http://www.` and `https://www.` separately; they should all end at the same address.
4. Open the **Network** tab in the browser's developer tools and reload the page; the status code of the first row should be `301`.

## 7. Symptom → cause → fix

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| "Too many redirects" (ERR_TOO_MANY_REDIRECTS) | The same redirect in two places (e.g. WordPress settings and `.htaccess`), or a proxy in front of you that terminates HTTPS | Remove one of the rules; if you use a proxy, do the HTTPS redirect in one place only |
| I added it in Redirects but the old address returns 404 | The rule is below the WordPress block | Move the lines above `# BEGIN WordPress` (Section 4) |
| The whole site shows 500 after saving | A typo in `.htaccess` | Put the backup back; find the wrong line on the Errors screen (**B41**) |
| It goes to HTTPS but shows a certificate warning | No valid SSL on the domain | Wait for or check the SSL installation, then turn on the redirect |
| The redirect sometimes works, sometimes shows the old page | Browser cache | Test in a private window |
| The www address doesn't open at all | No DNS record for `www` | Add a CNAME or A record for `www` in Zone Editor (**B48**) |

## 8. Frequently asked questions

**Should I use 301 or 302?**

301 if the address has changed for good. 302 for temporary situations such as a short campaign or maintenance. If you use 302 by mistake, search engines keep the old address in their index.

**Is the www or the non-www address better?**

Both are fine; what matters is choosing one and redirecting the other to it with a 301. Keep your choice the same in tools such as Search Console and in your site's settings.

**What else should I do when changing the address in WordPress?**

In the WordPress admin, make the site addresses on **Settings** → **General** match the new address. Otherwise WordPress and `.htaccess` can redirect in opposite directions and create a loop.

**How do I remove a redirect?**

If you added it with the Redirects tool, use **Delete** in the table. Delete rules you added by hand from `.htaccess` and save. Browsers may remember a 301 for a while; test in a private window.

**Can I redirect another domain to this site?**

Yes. Add the other domain to your account (if your plan's domain count allows it), then choose that domain in the Redirects tool and redirect it to the new address.

> **Did you know?**
> 
> If you manage your domain's DNS in Gyro DNS, you can also manage the `www` record and your other records with the API or Terraform; Gyro DNS is free for hosting customers (details in guide **L09**).

## 9. Related guides

- **B41** — My site won't load: 500 Internal Server Error (.htaccess errors)
- **B48** — Editing DNS records with cPanel Zone Editor
- **B47** — Hotlink protection
- **B09** — My site returns 403 Forbidden
