我想如果我的访问者去subdomain.example.com
,他们会被重定向到anothersubdomain.example.com
。如果他们去css.subdomain.example.com
他们重定向到css.anothersubdomain.example.com
等正确地重定向访问者(PHP和正则表达式)
我试过以下的正则表达式(用的preg_match):
尝试1:
if(preg_match('#(([\w\.-]+)\.subdomain|subdomain)\.example\.com#', $_SERVER['SERVER_NAME'], $match)) {
header('Location: http://'.$match[1].'anothersubdomain.example.com/');
}
如果他们去:subdomain.example.com
他们得到重定向到:anothersubdomain.example.com
但是,如果他们去:css.subdomain.example.com
他们也重定向也t ○:subdomain.example.com
- 所以不工作
尝试2:
if(preg_match('#([\w\.-]+)\.subdomain\.example\.com#', $_SERVER['SERVER_NAME'], $match)) {
header('Location: http://'.$match[1].'.anothersubdomain.example.com/');
}
如果他们去:css.subdomain.example.com
他们重定向到:css.anothersubdomain.example.com
,但如果他们去:subdomain.example.com
他们得到重定向到:.subdomain.example.com
- 并且该URL无效,所以此尝试也不起作用。
有人有答案吗?我不想使用nginx或apache重写。
在此先感谢。
+1 。我称之为TDA - “测试驱动的答案”。 ) – raina77ow
谢谢你完美的作品!我的名誉太小,否则+1 – user1480019
@nickb只有一件事情是关于带有连字符的子域(css-test.example.com)?和一个双点(subdomain.subdomain.example.com)子域? – user1480019