Skip to main content
drleung.net
share, exchange and connect

Main navigation

  • Home
  • About
User account menu
  • Log in

Redirect to use non-www URL

By drleung , 21 March 2026

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();
}

Tags

  • site
  • Log in to post comments

Comments

Quick Links

YouTube | CNN | BBC |

© 2026 drleung.net All Rights Reserved. All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.