How can I redirect urls using host file

Viewed 34

I have a link https://gitHub.com/path/to/file.tar.gz

I am trying to make a local link to non existent file.tar.gz. I know that I can't redirect the full path but maybe I can redirect https://github.com to localhost then I put corresponding folders to the full path. Is it possible to use /etc/hosts to achieve this and do I need extra server configurations to get the redirect recognisable in my web browser

What do I need to setup and how do I redirect https://github.com to localhost in hosts file?

1 Answers

First of all you need to have a web server like Nginx, Apache, … on your server, without it your server cannot process http requests from web browser.

Then you can set redirection for a specific pat -> Let’s assume you are using Nginx, one of the many ways to do redirection is by manipulating Nginx configuration located at

/etc/nginx/nginx.conf or /etc/nginx/sites-enabled/<YourDomainName>.conf

find your config file and edit it to:

server{
...
    location /github {
        rewrite ^/github(.*)$ https://www.github.com/$1 redirect;
    }
...
}

This will redirect all traffic from /github path on your server to https://www.github.com Then run command to restart Nginx service:

sudo service nginx reload #debian/ubuntu

Or

systemctl restart nginx #redhat/centos

That's it.

Related