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");
    
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装