检查URL是否在特定域下的正则表达式


Regular Expression to check if the URL is under a particular domain

我需要一个正则表达式来查找给定的URL是否在我的网站下。

例如:

http://www.my_domain.com/internal_page should give true.
www.my_domain.com/internal_page should give false.
http://www.my_domain.com should give false. ( should have a "/" after .com  ).
http://www.my_domain.com:dfdf should give false.

——谢谢你

下面是你想要的样式,

  $pattern = "#^https?://www.my_domain'.com(/.*)?$#";
  $test    = 'http://www.google.com';
  if ( preg_match( $pattern, $test ) )
  {
      echo $test, " <strong>matched!</strong><br>";
  } else {
      echo $test, " <strong>did not match.</strong><br>";
  }

使用

http:'/'/www'.my_domain'.com'/.*

你真的应该在发帖之前表现出一些努力。

  • http:'/'/ -表示http://

  • www'.my_domain'.com = www.mydomain.com

  • '/.*表示/和0或表达式末尾的任意字符。

这个怎么样?

http:'/'/www'.my_domain'.com'/(.+)

它通过了你给的四项测试

你可以这样做

 $url = "http://www.my_domain.com/internal_page";
 $host = "www.my_domain.com";
 if(strpos($url, "http://".$host."/") === 0){
      // we have a winner
 } else {
      // no good
 }