Forum Moderators: phranque
So I had a go at replacing it, my code didn't work well so i found some code on here and used that and it works fine, the problem is to my way of thinking it shouldn't work, (i am an absolute novice at mod rewrite stuff)
But this part (:[0-9]+) to my mind should only redirect numerical thingies of a variable length but it seems to redirect everything, which is what i want, but I can't figure why it works, and i can't find anything about the colon (:), in the mod rewrite tutorials i have been reading
Original Cpanel generated code
RewriteCond %{HTTP_HOST} ^nzmysite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.nzmysite.com$
RewriteRule ^/?$ http://mysitenz.co.nz [R=301,L]RewriteCond %{HTTP_HOST} ^nzmysite.co.nz$ [OR]
RewriteCond %{HTTP_HOST} ^www.nzmysite.co.nz$
RewriteRule ^/?$ http://mysitenz.co.nz [R=301,L]RewriteCond %{HTTP_HOST} ^mysitenz.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysitenz.com$
RewriteRule ^/?$ http://mysitenz.co.nz [R=301,L]RewriteCond %{HTTP_HOST} ^mysite.co.nz$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite.co.nz$
RewriteRule ^/?$ http://mysitenz.co.nz [R=301,L]
Replaced with (working code)
RewriteCond %{HTTP_HOST} ^www.mysitenz\.com(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^mysitenz\.com(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www.nzmysite\.com(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^nzmysite\.com(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www.nzmysite\.co\.nz(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^nzmysite\.co\.nz(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite\.co\.nz(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.co\.nz(:[0-9]+)?$
RewriteRule (.*) http://mysitenz.co.nz/$1 [R=301,L]
[edited by: jdMorgan at 3:25 am (utc) on Dec. 17, 2007]
[edit reason] de-linked [/edit]
"(:[0-9]+)?" means "a literal colon, followed by one or more digits, and all of this is optional"
Your new rules could benefit from the "(x)?" construct as well, to reduce the number of rules by half:
RewriteCond %{HTTP_HOST} ^(www\.)?mysitenz\.com(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?nzmysite\.com(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?nzmysite\.co\.nz(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co\.nz(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysitenz\.co.nz(:[0-9]+)?$
RewriteRule (.*) http://mysitenz.co.nz/$1 [R=301,L]
The purpose of the "(:[0-9]+)?" subpattern is to match an optionally-appended (and perfectly-valid) port number. Without this subpattern, the rule would break if the port number was appended, due to the fact that the hostname patterns are all end-anchored. You could leave the "(:[0-9]+)?" subpattern off, as long as you also remove the "$" end anchor from the pattern.
Jim