Search code examples
.htaccessnameservers

How to point domain to url on external site using nameservers


Here is what I want to accomplish.

I have domain A which I want to update the nameservers to use the custom nameservers setup on domain B (i.e. ns1.domainb.com and ns2.domainb.com).

Then, when users go to domainA.com I want it to take them to domainB.com/domainA

How can I accomplish this? Can't I accomplish this via HTACCESS files on domainB?


Solution

  • When users go to domainA.com I want it to take them to domainB.com/domainA

    This solution explains how to achieve:

    example.com --> domainB.com/example.com

    DNS records and .htaccess

    First, set the DNS of domainA.com to point to the IP address of domainB.com's server. In the root of domainB.com, create an .htaccess file with a rule to redirect/rewrite domainA.com requests to domainB.com/domainA. There are two methods and outcomes.

    Method 1 - Silently rewrite the URI request

    User navigates to http://example.com, browser shows http://example.com.

    Set these .htaccess rules in the root of domainB.com:

    RewriteEngine On
    RewriteBase /
    
    # Check that the request is NOT from domainB.com, e.g. domainA
    RewriteCond %{HTTP_HOST} !domainB\.com$ [NC]
    
    # Capture the top-level domainA name
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)$
    
    # Check that we are not already requesting a valid file or directory
    # This prevents inserting the subdirectory name into an already valid path
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # Rewrite requests with the domainA subdirectory
    RewriteRule (.*) /%1/$1 [L]
    

    Method 2 - Explicitly force a redirect to the new location

    User navigates to http://example.com, browser shows http://domainB.com/example.com.

    Set these .htaccess rules in the root of domainB.com:

    RewriteEngine On
    RewriteBase /
    
    # Check that the request is NOT from domainB.com, e.g. domainA
    RewriteCond %{HTTP_HOST} !domainB\.com$ [NC]
    
    # Capture the top-level domainA name
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)$
    
    # Force a 301 redirect to the new URI
    RewriteRule ^(.*)$ http://domainB.com/%1%{REQUEST_URI} [R=301,L]
    

    Customization

    At the time of this writing, you can test and modify (most) .htaccess rules to support your customized requirements here: http://htaccess.madewithlove.be/

    Here are tips and tricks to achieve further customization: http://mod-rewrite-cheatsheet.com/