Forum Moderators: phranque
if(preg_match("/mydomain\.com/i",$url)){}
However, I'd like to improve this, to get rid of matches like [notmine.com...]
How can I fix my regex statement to match if my domain.com comes before a possible third forward slash?
Thanks
should do the job, although that wouldn't work for test.www.mydomain.com -- but if that's alright with you, it should catch both, mydomain.com and www.mydomain.com, with both http and https as a protocol.
the ¦ in the parantheses is an OR-operator, which I hope php knows in the preg_match-function. I know this is the way to go in perl, but the "perl compatible regular expression"-functions in php aren't really a 100% compatible, so if it doesn't work, that might be the issue.
edit: forgot to mention, due to the forum software's parsing, make sure to replace the ¦ with a pipe, like in an if-clause for OR.
/^https?:\/\/([^.:\/]+\.)*example\.com(\.¦\.?:[0-9]+)?/ matches
example.com
example.com.
example.com:80
example.com.:80
www.example.com
www.example.com.
www.example.com:80
www.example.com.:80
test.example.com
www.test.example.com.:80
etc. (these are all perfectly-valid hostnames), but matching stops if a third slash is encountered.
So, it won't match
www.exampleother.com/example.com
www.example.commm/www.example.com, etc.
Jim
As g1smd noted above, you should be detecting requests for all of the non-canonical variants and 301-redirecting them to the canonical hostnames. But that is not necessarily the question that you asked, and you didn't provide enough details on the purpose of this code to allow anything but a specific answer in the form of a regex pattern.
Jim