WordPress, random

.htaccess wildcat redirect

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [L,R=301,NC]
  • RewriteEngine On: This line enables the Apache mod_rewrite module.
  • RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]: This line checks if the requested host (domain) is equal to your old domain without the “www”. The [NC,OR] flags mean it’s not case-sensitive and there’s another condition to check (the “OR” flag).
  • RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]: This line checks if the requested host (domain) is equal to your old domain with the “www”. The [NC] flag means it’s not case-sensitive.
  • RewriteRule ^(.)$ https://www.newdomain.com/$1 [L,R=301,NC]: This line creates a rule that matches any URL path (indicated by ^(.)$) and redirects it to the new domain with the same path (indicated by /$1). The [L,R=301,NC] flags mean that this is the last rule to process, it’s a 301 (permanent) redirect, and it’s not case-sensitive.