Forum Moderators: phranque
# Redirect to remove "www." if present, also removing FQDN period and port
RewriteCond %{HTTP_HOST} ^www\.([^.:]+(\.[^.:])+)\.?(:[0-9]+)?$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#
# Redirect to add "www." if not present, also removing FQDN period and port
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.:]+(\.[^.:])+)\.?(:[0-9]+)?$
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
#
# Follow-on rule for either variant above (to remove FQDN and port if "www" is already correct)
RewriteCond %{HTTP_HOST} ^([^.:]+(\.[^.:])+)(\.?:[0-9]+|\.)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]