Loading...

Indresh Verma

How to Create a Subdomain in Localhost | SEO-Friendly Guide

Step-by-step guide to set up a subdomain in localhost for your web development projects

Introduction

In this tutorial, we will walk you through the steps to create a subdomain in localhost. You will learn how to configure Apache or Nginx web servers to serve your subdomain locally. This setup is useful for web development and testing before going live.

Step 1: Edit the Hosts File

To make sure your system resolves the subdomain to your local machine, you'll need to modify the hosts file on your computer.

  • **Windows**: Open `C:\Windows\System32\drivers\etc\hosts` with administrator privileges.
  • **macOS/Linux**: Open the file `/etc/hosts` with root privileges.

Add the following line to map the subdomain to your local machine:

127.0.0.1   subdomain.localhost

Save the file, and you're ready to proceed.

Step 2: Configure Your Web Server

Next, you'll configure either Apache or Nginx to handle requests for your subdomain.

Apache Configuration

If you're using Apache, open the `httpd-vhosts.conf` file and add the following VirtualHost configuration:

<VirtualHost *:80>
    ServerName subdomain.localhost
    DocumentRoot "C:/path_to_your_project_folder"
    <Directory "C:/path_to_your_project_folder">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Don't forget to restart Apache to apply the changes.

Nginx Configuration

If you're using Nginx, edit the `nginx.conf` file or create a new server block in the `sites-available` directory:

server {
    listen 80;
    server_name subdomain.localhost;
    root /path_to_your_project_folder;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

Restart Nginx to apply the changes.

Step 3: Testing Your Subdomain

Open your browser and navigate to subdomain.localhost. If everything is configured correctly, you should see your web page served from the specified directory.

Troubleshooting

If your subdomain isn't working, ensure the following:

  • Check your Apache or Nginx configuration for any errors.
  • Ensure the `hosts` file is correctly edited and saved.
  • Clear your browser cache or use an incognito window.

Advanced Color Picker | Choose and Save Your Perfect Color Palette

Advanced and user-friendly color picker tool for web designers and developers. Choose, save, and explore color palettes and harmonies effortlessly.

Free JPG to PDF Converter - Convert JPG to PDF Online for Free

Convert JPG images to PDF online for free. No watermark, no registration. Fast, secure & easy to use. JPG to PDF, Image to PDF, PDF converter

How to Improve Cursor Speed and Boost Laptop Performance | Acer R3 131-T

Learn how to improve cursor speed and boost the performance of your Acer R3 131-T laptop with these simple steps. Optimize settings, upgrade hardware, and enhance your device today.