How to Create .htaccess 301 Redirect Rules: Complete Apache Guide

SEOFree Editorial Team Technical SEO Specialist
Published: September 20, 2026
5 min read

1. What Is the .htaccess File in Web Hosting?

The .htaccess (Hypertext Access) file is a directory-level configuration file supported by Apache and LiteSpeed web servers. It allows webmasters to manage URL routing, security headers, compression, caching lifetimes, and 301 redirects directly at the server level before PHP execution begins.

2. Essential .htaccess Redirect Recipes

  • Force HTTPS:
    RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  • Redirect www to non-www:
    RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
  • Single Page 301 Redirect:
    Redirect 301 /old-page /new-page
  • Normalize Trailing Slashes on Single Slugs:
    RewriteCond %{REQUEST_METHOD} =GET RewriteCond %{REQUEST_URI} !^/(admin|install|blog)/ [NC] RewriteRule ^([a-zA-Z0-9_-]+)/$ /$1 [R=301,L]

3. Automated Generator Tool

Avoid syntax errors and server 500 crashes by using our free Redirect Rule Generator to formulate verified Apache mod_rewrite code blocks.

SEOFree Technical Review

This guide was researched, tested, and authored by the SEOFree Editorial Team in accordance with Google Search Essentials and technical SEO best practices.

Recommended Free SEO Tools

Use these free utilities to implement the workflows discussed in this guide.

Generate Apache .htaccess RewriteRule directives for 301 permanent redirects, HTTPS, and migrations.

Inspect HTTP redirect hops, detect multi-hop chains, and diagnose infinite redirect loops.

Generate bulk HTML hreflang tags, XML sitemap annotations, and HTTP Link headers.

Audit live webpage hreflang annotations, verify reciprocal return links, validate ISO codes, and inspect canonical consistency.

Related SEO Guides & Tutorials

Explore more topic cluster articles to deepen your search optimization knowledge.