Forum Moderators: phranque

Message Too Old, No Replies

What does the "colon" do in redirects (:[0-9]+)

My redirect works perfectly, but i don't understand why

         

nigelt74

11:47 pm on Dec 16, 2007 (gmt 0)

10+ Year Member



Ok we have 5 domain names and i had set Cpanel to autmatically redirect them to one non WWW domain name, today i noticed that the redirect doesn't work for the sub-domains

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]

jdMorgan

1:05 am on Dec 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be reading regular-expressions tutorials [etext.lib.virginia.edu], then... :)

"(:[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]

Here, we use the same technique to make the "www." optional, except in the last RewriteCond.

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

nigelt74

3:13 am on Dec 17, 2007 (gmt 0)

10+ Year Member



So the colon was a complete red herring in my case, well at least for the purpose i am using it for,
Thanks for taking the time out to explain it for me, also for the bit about the the "(x)?" construct as I didn't know about that, but had been looking unsuccessfully for that functionality