: Forwarding www.example.com/abc to abc.example.com I have a web directory that can be accessed via either http://www.example.com/abc or http://abc.example.com. Same physical directory, same contents,
I have a web directory that can be accessed via either www.example.com/abc or abc.example.com. Same physical directory, same contents, just different addresses.
I want to redirect to abc.example.com if it is accessed via example.com/abc. Can I do it by a simple PHP script?
The index.php will be the contents for either address anyway.
More posts by @Jennifer507
2 Comments
Sorted by latest first Latest Oldest Best
It would be much better to configure the webserver to handle this redirection. It is fairly trivial to configure Apache to do something along these lines. If you are using Apache, look up RewriteRule. Most other web servers have comparable features.
You can analyze the $_SERVER['HTTP_HOST'] variable in PHP:
if( $_SERVER['HTTP_HOST'] !== 'abc.example.com' ){
header('Location: abc.example.com/', true, 301);
exit;
}
If it's not abc.example.com it will redirect to it.
I guess you can put it into your index.php at the very top.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2025 All Rights reserved.