diff --git a/core/control/RootURLController.php b/core/control/RootURLController.php index 2a4961d9c..a5ac38aa7 100755 --- a/core/control/RootURLController.php +++ b/core/control/RootURLController.php @@ -36,8 +36,8 @@ class RootURLController extends Controller { static function get_homepage_urlsegment() { $host = $_SERVER['HTTP_HOST']; $host = str_replace('www.','',$host); - $SQL_host = Convert::raw2sql($host); - $homePageOBJ = DataObject::get_one("SiteTree", "HomepageForDomain = '$SQL_host'"); + $SQL_host = str_replace('.','\\.',Convert::raw2sql($host)); + $homePageOBJ = DataObject::get_one("SiteTree", "HomepageForDomain REGEXP '(,|^) *$SQL_host *(,|\$)'"); if($homePageOBJ) { return $homePageOBJ->URLSegment; diff --git a/tests/control/RootURLControllerTest.php b/tests/control/RootURLControllerTest.php new file mode 100644 index 000000000..9eb0e88b4 --- /dev/null +++ b/tests/control/RootURLControllerTest.php @@ -0,0 +1,42 @@ + 'page1', + 'www.page.co.nz' => 'page1', + 'help.com' => 'page1', + 'www.help.com' => 'page1', + 'something.com' => 'page1', + 'www.something.com' => 'page1', + + 'other.co.nz' => 'page2', + 'www.other.co.nz' => 'page2', + 'right' => 'page2', + 'www. right' => 'page2', + + 'only.com' => 'page3', + 'www.only.com' => 'page3', + + 'www.somethingelse.com' => 'home', + 'somethingelse.com' => 'home', + + // Test some potential false matches to page2 and page3 + 'alternate.only.com' => 'home', + 'www.alternate.only.com' => 'home', + 'alternate.something.com' => 'home', + ); + + foreach($tests as $domain => $urlSegment) { + $_SERVER['HTTP_HOST'] = $domain; + $this->assertEquals($urlSegment, RootURLController::get_homepage_urlsegment(), "Testing $domain matches $urlSegment"); + } + + $_SERVER['HTTP_HOST'] = $originalHost; + } +} \ No newline at end of file diff --git a/tests/control/RootURLControllerTest.yml b/tests/control/RootURLControllerTest.yml new file mode 100644 index 000000000..6ca1978f4 --- /dev/null +++ b/tests/control/RootURLControllerTest.yml @@ -0,0 +1,15 @@ +Page: + home: + Title: Home + page1: + Title: First Page + URLSegment: page1 + HomepageForDomain: page.co.nz, help.com,something.com, www.something.com + page2: + Title: Second Page + URLSegment: page2 + HomepageForDomain: other.co.nz, right + page3: + Title: Third Page + URLSegment: page3 + HomepageForDomain: only.com \ No newline at end of file