The following code is intended to be put at the beginning of the "index.php" file in order to redirect www-URL to the non-www -URL version. In an Apache web server, the "index" file refers to a default or entry point document that the server serves when a directory is requested by a client (e.g., through a web browser) and no specific file name is provided. This behavior allows directories to be browsed more seamlessly. In Drupal and Wordpress CMS, both employ "index.php" as the entry point document in server home directory (.e.g. web or public_html) as they are both written in PHP.
// www to non-www rediect php code added
if ($_SERVER['HTTP_HOST']=="www.example1.com") {
header ("Location: http://example1.com".$_SERVER['REQUEST_URI']);
exit();
} else if ($_SERVER['HTTP_HOST']=="www.example2.com") {
header ("Location: http://example2.com".$_SERVER['REQUEST_URI']);
exit();
}
Comments