Skip to main content
Cloud Hosting starting at $2.91/mo. Auto-scaling servers with a FREE domain included.Start Now →
Tutorials

How to Create a Redirect in .htaccess: 301, 302 & Wildcard Examples

The .htaccess file is one of the most powerful tools on an Apache web server. This tutorial covers the most common redirect patterns with copy-paste examples you can use immediately.

S

Serverlys Team

May 22, 2026 · 6 min read

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

  1. Log in to your cPanel dashboard.
  2. Go to File Manager.
  3. Navigate to public_html (your website root).
  4. Look for the .htaccess file. If you do not see it, click Settings in the top-right corner and check Show Hidden Files (dotfiles).
  5. 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
301PermanentPage has moved permanentlyPasses ~90% of link equity
302TemporaryPage is temporarily unavailableDoes not pass link equity
307Temporary (strict)Same as 302 but preserves HTTP methodDoes not pass link equity
308Permanent (strict)Same as 301 but preserves HTTP methodPasses 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

  1. Use curl to test: Run curl -I https://yourdomain.com/old-page in your terminal. Look for the Location: header and the HTTP status code (301 or 302).
  2. Browser developer tools: Open the Network tab (F12), visit the old URL, and check the response headers for the redirect status and destination.
  3. Online tools: Use httpstatus.io or redirect-checker.org to test redirect chains.
  4. 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

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.

Related Tutorials

Full .htaccess support on every plan

Serverlys hosting runs on Apache/LiteSpeed with complete .htaccess support. Free SSL, free domain, and expert support when you need it.