Common htaccess redirects

Published: September 11, 2014 | Last Modified: April 27, 2022

REDIRECT NON-WWW to WWW

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

REDIRECT WWW TO NON-WWW

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

301 REDIRECT RULE - ALL PAGES TO ROOT OF NEW DOMAIN

RewriteEngine On
RewriteRule ^(.*)$ https://newdomain.com/ [R=301]


BASIC PAGE 301 REDIRECT RULE

Redirect 301 /about_us.php http://www.domain.org/about-us

301 REDIRECT MATCH RULE - ALL PAGES IN SUB-FOLDER TO 1 X NEW PAGE

RedirectMatch 301 ^/sub(.*)$ http://www.website.com/page.html

REDIRECT 301s AS REWRITE RULE

Redirect 301 /about_us.php http://www.domain.org/about-us

BECOMES

RewriteRule ^about_us\.php  http://www.domain.org/about-us? [L,R=301]

PARAMETER URLS - REDIRECTS

Redirect 301 /product_detail.php?cat=2&id=41 http://www.domain.org/gifts/product

BECOMES

RewriteCond %{QUERY_STRING}  id=41
RewriteRule ^product_detail\.php http://www.domain.org/gifts/product? [L,R=301]

REDIRECT A DIRECTORY, BUT NOT SUB-DIRECTORIES (301)

RewriteCond %{REQUEST_URI} ^/olddirectory[/]?$
RewriteRule (.*) /newlocation/ [R=301,L]