Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change my hosts file to route to a folder and not an ip

Fairly new freelance developer and I'm looking to add a local version of a website im working on for a dev environment.

I've got my localhost set up.

127.0.0.1 localhost

The syntax that I was using but is evidently not correct is

127.0.0.1/wordpress blahblahsite.ca

What route should I take to preserve my localhost, but have blahblahsite.ca route to my local folder and not the live site?

like image 584
monkeymanbob Avatar asked Nov 28 '13 21:11

monkeymanbob


People also ask

How do I change my host file to redirect?

Tips on Editing the Hosts File Inserting a redirect into the hosts file is simple. Just enter the IP address of the desired server or host, leave a single space, and then enter the domain or hostname you want to point to that IP. Enter only one redirect per line.

Can you specify port in hosts file?

Unfortunately, we can't. The hosts file only deals with hostnames, not ports.


2 Answers

Although its been couple of years since question was answered. My two pennies if any one stumble upon this question and really want to setup quickly without digging into documentation. Here goes: Enter this in to hosts file:

127.0.0.1 blahblahsite.ca

And in your apache folder, find httpd-vhosts.conf file. In a typical xampp installation on windows, you'll find here:

Drive:\xampp\apache\conf\extra\httpd-vhosts.conf

You should find couple or more eample virtual host setups commented out, just copy/paste or uncomment one of those and make changes as below:

<VirtualHost blahblahsite.ca:80>
    ServerAdmin [email protected]
    DocumentRoot "Drive:\xampp\htdocs\wordpress"
    ServerName blahblahsite.ca
    ServerAlias blahblahsite.ca
    ##ErrorLog "logs/blahblahsite.ca.log"
    ##CustomLog "logs/blahblahsite.ca.log" combined
</VirtualHost>

Remember to change: "Drive:\xampp\htdocs\wordpress" to your website files directory. Save files, restart xampp and now blahblahsite.ca should work from browser's address bar. Although steps are pretty easy and self explanatory, but do let me know if you have any questions or encounter and problems.

like image 95
Asad Malik Avatar answered Oct 04 '22 22:10

Asad Malik


As said in a comment, the hosts file has the function of mapping ip addresses to hostnames, i.e. 127.0.0.1

Hostnames have the function of being human friendly aliases for ip addresses, i.e. blahblahsite.ca

A hostname is synonymous with a host header in an URL to a web server, i.e. http://blahblahsite.ca

The host header has two functions:

  • resolving the request to the ip address of the web server. This is done through a dns query, or in your case through the hosts file which takes precedence over the dns query.
  • when the request arrives at the web server, mapping the request to the correct virtual host for further processing. This is done through the default virtual host (catch-all) or through a name based virtual host (where the servername or serveralias is set to blahblahsite.ca)

A URL to a webserver has a slash something following the host header. This is called a URI. The URI is the relative path in your web server to the page which is requested. The URI can be mapped through a multitude of mechanisms, but mirrors in its simplest form the subdirectories of the directory your web servers virtual host shares.

Thus, /wordpress is a URI shared by your web server in one way or another.

Principle of the URL: Protocol://hostHeader/URI

http://127.0.0.1/wordpress
http://blahblahsite.ca/wordpress

would both be valid URL:s on your local computer, if it has a web server sharing the URI /wordpress to any host header or to the host header blahblahsite.ca specifically, and has the following entry in its hostfile:

127.0.0.1 blahblahsite.ca

So, get a really good beginners book on Apache, read and try your wings, then move on to xampp. The URI is where the interesting stuff happens, but you need some web server basics in place first.

like image 32
ErikE Avatar answered Oct 04 '22 20:10

ErikE