Redirecting a domain or subdomain (e.g., login.example.com) to another (e.g., abc.example.com) can be done using various approaches. Below, we outline the steps to accomplish this on IIS (Internet Information Services), as well as alternative methods using .htaccess, Nginx, DNS, PHP, and Cloudflare for those using different setups.

Redirecting domains in IIS

Option 1: Using IIS Manager

  • Open IIS Manager:

Launch IIS Manager on your server.

In the Connections pane, locate and select the site corresponding to the subdomain login.example.com.

  • Enable HTTP Redirect:

In the Features View, double-click on HTTP Redirect.

IIS HTTP Redirect URL to another
  1. Check the box for Redirect requests to this destination, and enter: https://abc.example.com
  2. Under Redirect Behavior:
  3. Check Redirect all requests to exact destination (rather than relative to destination).
  4. Select Permanent (301) as the redirect type.
IIS HTTP Redirect URL to another
  • Apply Settings:

Click Apply in the right-hand pane to save your changes.

  • Restart IIS:

To apply the changes, restart IIS by running the following command in the command prompt: iisreset

Option 2: Edit the Web.config File

If you prefer to configure the redirect manually, you can use a web.config file. Place this file in the root directory of the login.example.com site:

<configuration>
    <system.webServer>
        <httpRedirect enabled="true" destination="https://abc.example.com" httpResponseStatus="Permanent" />
    </system.webServer>
</configuration>

Note: Ensure the HTTP Redirect module is installed on the server (see below if it isn’t).

Install the HTTP Redirect Module (If Necessary)

If the HTTP Redirect feature is not available in IIS Manager:

  1. Open Server Manager.
  2. Go to Add Roles and Features.
  3. In the Server Roles section, expand Web Server (IIS) > Web Server > Common HTTP Features.
  4. Check HTTP Redirect and complete the installation.

Alternative Methods for Redirecting Subdomains

1. Redirect domain using .htaccess (For Apache Servers)

If you’re using Apache, you can redirect the subdomain by adding this to the .htaccess file in the root directory of login.example.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com$ [NC]
RewriteRule ^(.*)$ https://abc.example.com/$1 [L,R=301]
  • Explanation:
    • RewriteCond checks if the request is coming to login.example.com.
    • RewriteRule redirects all requests to abc.example.com with the same path ($1).

2. Redirect domain using Nginx

For Nginx servers, add this configuration to the server block for login.example.com:

server {
    server_name login.example.com;

    return 301 https://abc.example.com$request_uri;
}

After updating the configuration, reload Nginx:

sudo systemctl reload nginx

3. Redirect domain at the DNS Level

If you want to redirect using DNS, follow these steps:

  1. Go to your DNS management panel.
  2. Add a CNAME record for login.example.com pointing to abc.example.com.
  3. If your DNS provider supports it, you can also use a URL redirection service to create a 301 redirect.

Note: DNS-level redirection typically works for simple cases and doesn’t preserve paths. You can check your DNS delegation by going to https://tools.itgranules.com/tools/dns-lookup

4. Redirect domain using PHP

If you have PHP enabled on the server for login.example.com, you can create an index.php file with the following content:

<?php
header("Location: https://abc.example.com", true, 301);
exit();

This will issue a 301 redirect to the target subdomain.

5. Redirect domain using Cloudflare

If you use Cloudflare for DNS management, you can set up a redirect via Page Rules:

  1. Log in to your Cloudflare account.
  2. Go to the Page Rules section.
  3. Add a new rule:
    • If the URL matches: https://login.example.com/*
    • Then the setting is: Forwarding URL → 301 Permanent Redirect
    • Redirect to: https://abc.example.com/$1
  4. Save the rule.

Testing the domain redirect

After setting up the redirect, verify it:

  1. Open a browser and navigate to http://login.example.com or https://login.example.com.
  2. Ensure it redirects to https://abc.example.com.
  3. Use tools like Redirect Checker to confirm the redirect status (301/302).

Conclusion

Redirecting one subdomain to another can be achieved through IIS configurations or alternative methods like .htaccess, Nginx, DNS, PHP, or Cloudflare. For IIS, using the HTTP Redirect module or editing the web.config file is the most straightforward solution. Choose the method that aligns best with your hosting environment and requirements.

How to redirect a domain to another in IIS, Apache, Nginx, DNS, PHP, Cloudflare

Post navigation