Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 952
chad Avatar asked Jul 19 '10 21:07

chad


People also ask

Can I redirect a domain to a specific URL?

What is URL redirect? URL redirect (URL forwarding) allows you to forward your domain visitors to any URL of your choice (to a new domain or a different website). You can set 301 (Permanent), 302 (Unmasked), and Masked (URL Frame) redirects for the domain names pointed to BasicDNS, PremiumDNS or FreeDNS.

Can you redirect any URL?

When you redirect a URL, you're simply forwarding it to another address on the same, or different domain. You can set up a redirect that sends visitors to your new domain name when they'll try to access a URL that belonged to your old domain.


2 Answers

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.

like image 165
Jim Lewis Avatar answered Oct 06 '22 01:10

Jim Lewis


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');
?>
like image 22
AbdullahDiaa Avatar answered Oct 06 '22 00:10

AbdullahDiaa