1. Overview

In Linux, we sometimes need to redirect a domain only in our current environment. For example, we might do some testing and troubleshooting with a domain name that is otherwise resolved globally. Of course, we can also perform redirection for convenience.

In any case, there are considerations and potential pitfalls that developers and administrators should be aware of when redirecting domains.

In this tutorial, we’ll delve deeper into the process of redirecting a certain domain, elaborate on common failure scenarios, and explore alternative solutions for more complex cases.

2. Modifying /etc/hosts

Before we explore various examples of domain redirection, let’s check some prerequisites for successful domain redirection using the /etc/hosts (hosts) file.

2.1. Hosts File Modification

The hosts file is a simple mapping between IP addresses and domain names. When redirecting a domain to another domain locally, we can manipulate this file to instruct the operating system to resolve specific domains to a given IP address. This process takes precedence over DNS lookup, allowing us to test redirection on our local machine.

2.2. Administrative Privileges

Since the hosts file is a system file within a protected directory, modifying it requires administrative or root access. In other words, using elevated privileges is essential to make changes to this file.

Let’s check how to log in as the superuser:

$ su - 
Password: 
Last login: Sun Nov 14 05:08:21 PDT 2022 on tty2 
# whoami 
root

In the above snippet, we utilize the su command followed by the symbol to switch to the root user. Furthermore, we ascertain the present user’s identity by using the whoami command.

2.3. Syntax and Formatting

The syntax of the hosts file is fairly straightforward. Particularly, it consists of an IP address followed by whitespace and one or more domain names. In addition, each domain name is separated by spaces or tabs.

Let’s check an example of the correct syntax:

# IP_ADDRESS   domain1.com   domain2.com

The above snippet is just a brief sample. Moreover, the values of the IP address and domain names vary depending on our needs and environment.

3. Correct and Incorrect hosts Entries

In this section, we’ll discuss the consequences of using an incorrect syntax format for the hosts file and compare it to the correct one.

3.1. Incorrect Format

In case we use an incorrect format, we might not get the results we aim for. Further, using an incorrect format can result in unexpected outcomes that are hard to troubleshoot.

Let’s delve into a failure scenario to illustrate this concept:

site1.dev  testsite1.dev

In this scenario, it might appear that we’re providing two domain names for redirection. However, the challenge here lies in the fact that the hosts file format requires an IP address followed by one or more domain names. Accordingly, this means that the first entry should be a valid IP address that corresponds to the domains we intend to redirect.

Since site1.dev isn’t a valid IP address, the system will be unable to interpret the entry correctly. Consequently, this results in the failure of the redirection attempt.

When we attempt to access site1.dev or testsite1.dev in a Web browser, the system fails to resolve the domains correctly. This results in the inability to achieve the desired redirection, which might appear as a failure to load the expected content or a site not found error in the browser.

3.2. Correct Format

To overcome the failure scenario described above, it’s essential to structure our hosts file entries correctly. The proper format of the hosts file requires an IP address followed by one or more host or domain names.

Let’s consider the following corrected example:

127.0.0.1  site1.dev  testsite1.dev

In this corrected format, the IP address 127.0.0.1 serves as the anchor point for the redirection. This IP address maps to the hostnames access site1.dev and testsite1.dev.

Once we’ve correctly updated the hosts file with the proper format, accessing either site1.dev or testsite1.dev in a Web browser should successfully redirect us to the desired destination – 127.0.0.1. The system now associates the localhost with these hostnames, allowing for effective domain redirection.

Alternatively, we might use the below command to have the same results:

$ echo "127.0.0.1 newexample.com" >> /etc/hosts

Next, let’s break down each part of the command:

  1. echo127.0.0.1 newexample.com” prints the IP address and the domain
  2. we use >> to append the /etc/hosts file
  3. /etc/hosts path points to the hosts file on Unix-like operating systems holding IP addresses mapped to their domain names

Particularly, we use this command to add an entry to the /etc/hosts file for domain redirection locally, without producing unnecessary terminal output.

The /etc/hosts technique takes advantage of the fact that behind any domain is an IP address. Still, a hosts file isn’t the ideal means to direct a domain to another domain, since it’s static and prone to errors.

Let’s see a potentially safer and better way to achieve our aims.

4. Using Web Servers

To perform domain redirection between two domains, we can also set up a simple web server that performs the redirection. In this section, we’ll use the Apache HTTP Server as an example.

4.1. Install Apache Web Server

To install the Apache web server, if not already installed, we use the apt command:

$ sudo apt update
$ sudo apt install apache2

In the above snippet, we use the sudo command to grant ourselves superuser privileges. This makes it possible to install the apache2 web server.

4.2. Configure Domain Redirection

Let’s imagine a scenario where we want to redirect example.com to newexample.com.

First, we can create an htaccess file if it doesn’t already exist in the root directory of our Apache site:

$ cat /var/www/html/.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://newexample.com/$1 [L,R=301,NC]

As cat shows, this code uses mod_rewrite to check if the requested domain is example.com or www.example.com. If either condition is met, the .htaccess file redirects the user to http://newexample.com while preserving the requested path.

4.3. Apply Settings

After configuring the redirection, we restart Apache to apply the changes:

$ sudo systemctl restart apache2

We used systemctl to issue the restart of the server. Now, if we access example.com or www.example.com in a web browser, it should automatically redirect to www.newexample.com.

Generally, when a client sends a request to the Apache server, Apache first checks the htaccess file in the requested directory and applies the rules defined therein.

In our example, it checks if the request matches the conditions specified in RewriteCond and, if so, applies the redirection defined in RewriteRule.

This combination of the htaccess file and Apache configuration settings allows for flexible, per-directory control over URL rewriting and redirection, making it a powerful tool for web developers to manage how requests are processed on websites.

5. Conclusion

In this article, we learned how to configure domain redirection locally using the hosts file, as well as a web server.

While the former may be a valuable technique for developers to simulate domain redirection without altering DNS settings, the latter helps for actual redirection between domains.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.