20

How do I redirect a URL to another URL in the hosts file, rather than redirecting an IP to a URL?

chad
  • 203
  • 1
  • 2
  • 4
  • 2
    possible duplicate of [Editing hosts file to redirect url?](http://stackoverflow.com/questions/3266483/editing-hosts-file-to-redirect-url) – David Jul 19 '10 at 21:34

2 Answers2

18

You can't. DNS (or the host files) lets you look up IP addresses for a given host name. There is no concept of remapping URLs at this level of networking. This needs to be done in your web server configuration.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
8

you can install localhost (MAMP ,LAMP ..etc) , and redirect all links to 127.0.0.1 , then create script to redirect to any website .

here's PHP example

<?php
function curPageURL() {
    $pageURL = "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}
// if pageURL is facebook , redirect to medium.com
if(curPageURL() == "http://www.facebook.com/")
    header('Location: http://www.medium.com');
?>
AbdullahDiaa
  • 1,316
  • 16
  • 17