Mobile app version of vmapp.org
Login or Join
Jennifer507

: 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,

@Jennifer507

Posted in: #DomainForwarding #Php

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.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Jennifer507

2 Comments

Sorted by latest first Latest Oldest Best

 

@Si4351233

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.

10% popularity Vote Up Vote Down


 

@Gretchen104

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme