du5591 2016-11-17 23:24
浏览 30

使用正则表达式测试URL重定向的所有可能的字母,数字或字符

I have a Joomla! site set up where multiple domains are being used for one Joomla instance. The root domain for the site is www.pky.com. When visiting any other domain name on the list of names we purchased they are directed to the root domain but I am using a php script to redirect to the appropriate domain name for the "site" being viewed by testing the URI. Some of the urls used for the site are exampleone.pky.com, exampletwo.pky.com, etc.

Our redirect script in the index.php file for the site is set up like so:

<?php
$domain = $_SERVER["HTTP_HOST"];
$requri = $_SERVER['REQUEST_URI'];


if ((($domain == "pky.com") || ($domain == "www.pky.com") || ($domain ==   "citywestplace.pky.com") || ($domain == "www.citywestplace.pky.com") || ($domain == "gwplazahouston.com") || ($domain == "www.gwplazahouston.com") || ($domain == "greenwayplaza.pky.com") || ($domain == "www.greenwayplaza.pky.com") || ($domain == "postoakhouston.com") || ($domain == "www.postoakhouston.com") || ($domain == "postoakcentral.pky.com") || ($domain == "www.postoakcentral.pky.com") || ($domain == "sanfelipeplaza.com") || ($domain == "www.sanfelipeplaza.com") || ($domain == "sanfelipeplaza.pky.com") || ($domain == "www.sanfelipeplaza.pky.com")) && ($requri == "/index.php/citywestplace-marketing")) { 
   header("Status: 301 Moved Permanently");
   header("Location: http://www.citywesthouston.com$requri"); 
}

One of our main issues is that someone entering in foo.pky.com will still show up to the site which in turn breaks the script. Is there a way to set a variable to a regex that fits all possible variations of letters, numbers, and other characters then use that variable as part of the url so that it is also redirected to the root domain?

For example:

$myvar = some regex here;

if (($domain == "pky.com") || ($domain == "www.pky.com") || ($domain == $myvar . ".pky.com") || ($domain == "www" . $myvar . ".pky.com"))...

There must be a better way to do all this but I am still learning.

Thank you in advance.

EDIT

So I have added:

if (((preg_match(`.*\.pky\.com`, $domain)) || (preg_match(`www\..*\.pky\.com`, $domain))) && ($menutype == "mainmenu")) {
   header("Status: 301 Moved Permanently");
   header("Location: http://www.pky.com");
}

to check if anything is being added in front of pky.com && the current menu type is "mainmenu". I checked the regex that Aziz had mentioned; however, entering:

foo.pky.com still goes to the url foo.pky.com and not redirected to pky.com.

  • 写回答

1条回答 默认 最新

  • dpj0015 2016-11-17 23:32
    关注

    Firstly you can get the domain from parse_url() then when you get the host you can use simple regex for example:

    $allowedUrls = [
      'test.com',
      'google.com'
    ];
    
    if (preg_match(`#.*pky\.com$#gm`, $domain) || in_array($domain, $allowedUrls)) {
       // OK
    }
    

    even without regex it's possible to check if domain ends with pky.com

     (substr($domain, -7) === "pky.com");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突