title: Using regex to test for all possible letters, numbers, or characters for url redirect
body: <p>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.</p>
  
  <p>Our redirect script in the index.php file for the site is set up like so:</p>
  
  <pre><code>&lt;?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")) &amp;&amp; ($requri == "/index.php/citywestplace-marketing")) { 
     header("Status: 301 Moved Permanently");
     header("Location: http://www.citywesthouston.com$requri"); 
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>For example:</p>
  
  <pre><code>$myvar = some regex here;
  
  if (($domain == "pky.com") || ($domain == "www.pky.com") || ($domain == $myvar . ".pky.com") || ($domain == "www" . $myvar . ".pky.com"))...
  </code></pre>
  
  <p>There must be a better way to do all this but I am still learning.</p>
  
  <p>Thank you in advance.</p>
  
  <p>EDIT</p>
  
  <p>So I have added:</p>
  
  <pre><code>if (((preg_match(`.*\.pky\.com`, $domain)) || (preg_match(`www\..*\.pky\.com`, $domain))) &amp;&amp; ($menutype == "mainmenu")) {
     header("Status: 301 Moved Permanently");
     header("Location: http://www.pky.com");
  }
  </code></pre>
  
  <p>to check if anything is being added in front of pky.com &amp;&amp; the current menu type is "mainmenu".  I checked the regex that Aziz had mentioned; however, entering:</p>
  
  <p>foo.pky.com still goes to the url foo.pky.com and not redirected to pky.com. </p>