URL Masking with DNS and/or Mod_Rewrite?

后端 未结 2 724
梦毁少年i
梦毁少年i 2021-01-07 06:34

I want to setup a system so that multiple custom domains like [mydomain.com/params] will redirect to [myapp.com/mydomain.com/params]. Can I do this using only DNS s

相关标签:
2条回答
  • 2021-01-07 06:47

    Here's the solution:

    1. Set DNS Address records for all vanity domains to the same IP address (so d1.com, d2.com, d3.com, etc. all have DNS A records set to one IP or FQDN for example)
    2. Setup the server with one VirtualHost using the IP as the domain
    3. Within that VirtualHost's root directory, create a .htaccess that sets up the mod_rewrite
    4. Use the following for the mod_rewrite in the .htaccess:
    Options +FollowSymLinks
    
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} (.*) [NC]
    RewriteRule (.*) http://myapp.com/%1/$1 [P,R=301,L]
    
    

    %1 = the domain that is requested, so www.d1.com or d1.com
    $1 = the rest of the URL that comes after the vanity URL (d1.com/everyting/else

    This config invisibly redirects all requests.

    Examples:

    d1.com => returns content from => myapp.com/d1.com/
    
    www.d1.com => returns content from => myapp.com/www.d1.com/
    
    d1.com/blog/post/1 => returns content from => myapp.com/d1.com/post/1
    
    0 讨论(0)
  • 2021-01-07 07:07

    No, you can't use only DNS for that.

    If every domain can run standalone (www.domain.com) this would be a straightforward multi-site setup and does not require mod_rewrite, just a bunch of <virtualHost> directives that point to each site.

    If you need exactly the setup you describe (http://www.hostname.com/www.2ndhostname.com/directoryname) you would need one <VirtualHost> with all the domains as aliases, and a mod_rewrite based redirect to point incoming requests to the right directory.

    0 讨论(0)
提交回复
热议问题