您可能会执行以下操作:
if (preg_match('#.*/articles/([a-z0-9\-_]+)\.php#i', $_SERVER['PHP_SELF'], $match)) {
header('Location: http://' . $match[1] . '.foo.com');
exit;
}
请记住在任何其他输出之前粘贴此代码,否则您将收到错误消息。
或通过.htaccess
启用mod_rewrite
和RewriteEngine ON
:
RewriteRule ^.*/articles/([a-z0-9\-_]+)\.php$ http://$1.foo.com [R,L]
编辑
反过来是这样的:
if (preg_match('#^http://([a-z0-9\-_]+)\.foo\.com.*$#i', $_SERVER['HTTP_HOST'], $match)) {
header('Location: http://foo.com/articles/' . $match[1] . '.php');
exit;
}
并与.htaccess
:
RewriteCond %{HTTP_HOST} ^([a-z0-9\-_]+).foo.com$
RewriteRule .* http://foo.com/articles/%1.php [R,L]