Redirects are essential for maintaining SEO when you move pages, change your domain structure, or force HTTPS. The .htaccess file on Apache web servers (used by most cPanel hosting providers, including Serverlys) is the standard way to create redirects without installing plugins or modifying application code.
This tutorial provides copy-paste redirect examples for the most common scenarios. Each example includes an explanation of what it does and when to use it.
Where to Find and Edit .htaccess
- Log in to your cPanel dashboard.
- Go to File Manager.
- Navigate to
public_html(your website root). - Look for the
.htaccessfile. If you do not see it, click Settings in the top-right corner and check Show Hidden Files (dotfiles). - Right-click the file and select Edit.
Important: Always create a backup of your .htaccess file before editing it. A syntax error in .htaccess will take your entire website offline with a 500 Internal Server Error. Copy the current contents to a text file on your computer before making changes.
Understanding Redirect Types
| Code | Type | When to Use | SEO Impact |
|---|---|---|---|
| 301 | Permanent | Page has moved permanently | Passes ~90% of link equity |
| 302 | Temporary | Page is temporarily unavailable | Does not pass link equity |
| 307 | Temporary (strict) | Same as 302 but preserves HTTP method | Does not pass link equity |
| 308 | Permanent (strict) | Same as 301 but preserves HTTP method | Passes link equity |
In most cases, you want a 301 redirect. Use 302 only when the redirect is genuinely temporary (e.g., a seasonal promotion page that will return).
Basic Redirect Examples
Redirect a Single Page
When you rename or move a page, redirect the old URL to the new one:
Redirect 301 /old-page https://yourdomain.com/new-page
The path /old-page is relative to your domain root. Do not include the domain name in the source path.
Redirect an Entire Directory
If you restructured your URL hierarchy and moved all pages from one directory to another:
Redirect 301 /old-directory/ https://yourdomain.com/new-directory/
This redirects all URLs under /old-directory/ to the corresponding paths under /new-directory/. For example, /old-directory/page1 redirects to /new-directory/page1.
Redirect to a Different Domain
If you changed your domain name and want all traffic to go to the new one:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
This preserves the URL path during the redirect. A visitor going to olddomain.com/about will be redirected to newdomain.com/about.
HTTPS and WWW Redirects
Force HTTPS (HTTP to HTTPS)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is essential after setting up SSL. It ensures all visitors use the secure HTTPS version of your site.
Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
Redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
Force HTTPS and www Together
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]
Wildcard and Pattern Redirects
Redirect All Pages to Home Page
Useful when shutting down a website and redirecting all traffic to a new site:
RewriteEngine On
RewriteRule ^(.*)$ https://newdomain.com/ [L,R=301]
Redirect Based on File Extension
Redirect all .html pages to their .php equivalents:
RewriteEngine On
RewriteRule ^(.+)\.html$ /$1.php [L,R=301]
Remove .html Extension from URLs
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^(.+)\.html$ /$1 [L,R=301]
Add or Remove Trailing Slash
Force trailing slash (recommended for WordPress):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
Remove trailing slash:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
WordPress-Specific Redirects
Redirect After Changing Permalink Structure
If you changed your WordPress permalink structure from date-based to post-name:
RedirectMatch 301 ^/\d{4}/\d{2}/(.*)$ https://yourdomain.com/$1
This redirects URLs like /2025/06/my-post to /my-post.
Redirect Category Base
RedirectMatch 301 ^/category/(.*)$ https://yourdomain.com/topics/$1
Redirect WordPress Login Page (Security)
Redirect the default login page to a custom URL (use with a login URL changer plugin):
RewriteRule ^wp-login\.php$ /my-secret-login [R=302,L]
"Place your redirect rules above the WordPress rewrite block in .htaccess (above the line that says # BEGIN WordPress). WordPress regenerates its own rules and may overwrite anything placed inside its block."
Testing and Debugging Redirects
- Use curl to test: Run
curl -I https://yourdomain.com/old-pagein your terminal. Look for theLocation:header and the HTTP status code (301 or 302). - Browser developer tools: Open the Network tab (F12), visit the old URL, and check the response headers for the redirect status and destination.
- Online tools: Use httpstatus.io or redirect-checker.org to test redirect chains.
- Check for loops: If you see ERR_TOO_MANY_REDIRECTS, you have a redirect loop. This usually means two rules are sending traffic back and forth between two URLs.
Common Mistakes to Avoid
- Redirect chains. A redirects to B, B redirects to C. Each hop adds latency and loses SEO value. Always redirect directly from A to C.
- Forgetting RewriteEngine On. The
RewriteEngine Ondirective must appear before any RewriteRule or RewriteCond directives. You only need it once per .htaccess file. - Using 302 when you mean 301. Search engines treat 302 as temporary and do not transfer link equity. If the redirect is permanent, always use 301.
- Redirecting to a page that does not exist. Always verify that the destination URL is live and returning a 200 status code before creating the redirect.
- Not clearing browser cache. Browsers cache 301 redirects aggressively. Use an incognito window or clear your cache when testing to ensure you see the current behavior.
Serverlys Tip: All Serverlys hosting plans run on Apache with full .htaccess support. If you need help with complex redirect rules, our support team can assist. View plans.
Frequently Asked Questions
Does .htaccess work on Nginx servers?
No. The .htaccess file is specific to Apache web servers. Nginx uses its own configuration format in server block files. If your hosting uses Nginx, you will need to create redirect rules in the Nginx configuration instead. Most cPanel-based hosting (including Serverlys) uses Apache or LiteSpeed, which both support .htaccess.
How many redirects are too many?
Each individual redirect adds a small amount of server processing time (typically 1–5ms). Having hundreds of redirect rules in .htaccess is fine. What you want to avoid is redirect chains where a single request triggers multiple sequential redirects. Google follows up to 10 redirects before giving up, but ideally every redirect should go directly to the final destination.
Do redirects affect page speed?
A single redirect adds roughly 50–200ms to page load time (mostly due to the additional DNS lookup and TCP connection). This is why you should minimize redirect chains and avoid unnecessary redirects on high-traffic pages.
Should I delete old redirect rules?
Keep 301 redirects in place for at least 1 year after the change. This gives search engines time to discover the redirect and update their index. After a year, the old URLs should no longer appear in search results, and you can safely remove the rules.
Can .htaccess redirects break my WordPress site?
Yes, if you make a syntax error. Always back up your .htaccess file before editing, and test your changes immediately after saving. If your site shows a 500 error, restore the backup version of .htaccess via File Manager or FTP.