# ============================================================================= # .htaccess STARTER — 301 vs 302 redirects, copy-ready # Source: https://www.kunaldabi.com/301-vs-302-redirections/ # Author: Kunal Singh Dabi · KD Digital · WhatsApp +91 96366 50036 # ============================================================================= # 99% of redirect bugs are: chains (>2 hops), wrong type (302 instead of 301 # for permanent moves), or HTTPS+www mismatch. This file fixes all three. # Drop into your site root and edit the rules block below. # ============================================================================= # Enable rewrite engine (needed for any redirect rule below to work) RewriteEngine On # ----------------------------------------------------------------------------- # 1. CANONICAL HOST — pick ONE and 301 everything else to it. # Without this, Google indexes both http://, https://, www., and apex — # splitting authority across 4 URLs for the same content. # Example: force https + www. Comment out the block you don't want. # ----------------------------------------------------------------------------- # https + www (recommended for most marketing sites) RewriteCond %{HTTPS} !=on [OR] RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE] # https + apex (no www) — uncomment if you prefer apex # RewriteCond %{HTTPS} !=on [OR] # RewriteCond %{HTTP_HOST} ^www\. [NC] # RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301,NE] # ----------------------------------------------------------------------------- # 2. PERMANENT REDIRECTS (301) # Use 301 for any URL that PERMANENTLY moved. Google passes ~99% of link # equity through 301s within 6 weeks. Once it's a 301 in production for # >6 months, treat it as locked — reverting to the old URL costs ranking. # Format: Redirect 301 /old-path/ https://www.example.com/new-path/ # ----------------------------------------------------------------------------- Redirect 301 /old-blog-post/ https://www.example.com/new-blog-post/ Redirect 301 /old-category/ https://www.example.com/new-category/ # (Add more rules above this line, keeping them grouped together.) # ----------------------------------------------------------------------------- # 3. TEMPORARY REDIRECTS (302) # Use 302 ONLY for temporary moves: A/B tests, geolocation tests, # seasonal landing pages that will revert. Google does NOT pass equity # through 302s — they're treated as "the old URL still exists, come back". # Format: Redirect 302 /promo/ https://www.example.com/promo-2026/ # ----------------------------------------------------------------------------- # Redirect 302 /summer-sale/ https://www.example.com/summer-sale-2026/ # ----------------------------------------------------------------------------- # 4. WILDCARD / DIRECTORY REDIRECTS # For moving a whole subtree. RedirectMatch supports regex. # ----------------------------------------------------------------------------- # Move everything under /blog/* to /articles/* # RedirectMatch 301 ^/blog/(.*)$ https://www.example.com/articles/$1 # Strip trailing index.html from URLs (canonical cleanup) # RewriteCond %{THE_REQUEST} \s/+(.*?)index\.html[\s?] [NC] # RewriteRule ^ /%1 [R=301,L] # ----------------------------------------------------------------------------- # 5. KILL CHAINS (avoid /a → /b → /c → /d) # If you replaced /old1/ with /old2/ in 2022 and now /old2/ is moving to # /new/ — change the original /old1/ rule to point AT /new/ directly, # so /old1/ → /new/ (1 hop), not /old1/ → /old2/ → /new/ (2 hops). # Google's crawler stops following after ~5 hops. Browsers slow noticeably # after 2. # ----------------------------------------------------------------------------- # ----------------------------------------------------------------------------- # 6. POST-DEPLOY: VERIFY EVERY RULE # Run the bash script in this kit (redirects-test.sh) to test all rules # with real HTTP HEAD requests. It checks status code + Location header # + final URL after following the chain. See: # https://www.kunaldabi.com/301-vs-302-redirections/#how-to-verify-dont-believe-it-prove-it # ----------------------------------------------------------------------------- # End of redirects block.