.htaccess 301 wildcard redirect old domain to new

Using mod_alias and RedirectMatch:

RedirectMatch 301 ^/.*$ https://www.newdomain.com/

Or using mod_rewrite:

RewriteEngine On
# If domain is not www.newdomain.com
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$
# Redirect to http://www.newdomain.com/wildcard and make it https too
RewriteRule ^/?(.*)$ https://www.newdomain.com/$1 [R=301,L]

However Apache documentation suggests not using mod_rewrite when a simpler alternative will suffice: When not to use mod_rewrite

Leave a Reply

Your email address will not be published.