Wildcard Subdomains

后端 未结 8 897
广开言路
广开言路 2020-12-07 15:25

I know there have been a few threads on this before, but I have tried absolutely everything suggested (that I could find) and nothing has worked for me thus far...

W

相关标签:
8条回答
  • 2020-12-07 15:37

    As far as how to set up the DNS subdomain wildcard, that would be a function of your DNS hosting provider. This would be different steps depending on which hosting provider you have and would be a better question for them.

    Once you've set that up with the DNS host, from your web app you really are just URL rewriting, which can be done with some sort of module for the web server itself, such as isapi rewrite if you're on IIS (this would be the prefered route if possible). You could also handle rewriting at the application level as well (like using routing if on ASP.NET).

    You'd rewrite the URL so http://myname.domain.com would become http://domain.com/something.aspx?name=myname or something. From there on out, you just handle it as if the myname value was in the query string as normal. Does that make sense? Hope I didn't misunderstand what you're after.

    Edit:

    I am not suggesting that you create a subdomain for each user, but instead create a wildcard subdomain for the domain itself, so anything.domain.com (basically *.domain.com) goes to your site. I have several domains setup with mydomain. Their instructions for setting this up is like this:

    Yes, you can configure a wild card but it will only work if you set it up as an A Record. Wildcards do not work with a C Name. To use a wildcard, you use the astericks character '*'. For example, if you create and A Record using a wild card, .domain.com, anything that is entered in the place where the '' is located, will resolve to the specified IP address. So if you enter 'www', 'ftp', 'site', or anything else before the domain name, it will always resolve to the IP address

    I have some that are setup in just this way, having *.domain.com go to my site. I then can read the base URL in my web app to see that ryan.domain.com is what was currently accessed, or that bill.domain.com is what was used. I can then either:

    1. Use URL rewriting so that the subdomain becomes a part of the query string OR
    2. Simply read the host value from the accessed URL and perform some logic based on that value.

    Does that make sense? I have several sites set up in just this exact way: create the wildcard for the domain with the DNS host and then simply read the host, or base domain from the URL to decide what to display based on the subdomain (which was actually a username)

    Edit 2:

    There is no way to do this without a DNS entry. The "online world" needs to know that name1.domain.com, name2.domain.com,...,nameN.domain.com all go to the IP address for your server. The only way to do this is with the appropriate DNS entry. You have to add the wildcard DNS entry for your domain with your DNS host. Then it's just a matter of you reading the subdomain from the URL and taking the appropriate action in your code.

    0 讨论(0)
  • 2020-12-07 15:37

    The solution I found for Ubuntu 18.04 is similar to this one but involves NetworkManager config:

    1. Edit the file /etc/NetworkManager/NetworkManager.conf, and add the line dns=dnsmasq to the [main] section

      sudo editor /etc/NetworkManager/NetworkManager.conf
      

      should look like this:

      [main]
      plugins=ifupdown,keyfile
      dns=dnsmasq
      ...
      
    2. Start using NetworkManager's resolv.conf

      sudo rm /etc/resolv.conf
      sudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf
      
    3. Create a file with your wildcard configuration

      echo 'address=/.localhost/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/localhost-wildcard.conf
      
    4. Reload NetworkManager configuration

      sudo systemctl reload NetworkManager
      
    5. Test it

      dig localdomain.localhost
      

    You can also add any other domain, quite useful for some types of authentication when using a local development setup.

    echo 'address=/.local-dev.workdomain.com/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/workdomain-wildcard.conf
    

    Then this works:

    dig petproject.local-dev.workdomain.com
    
    ;; ANSWER SECTION:
    petproject.local-dev.workdomain.com. 0 IN   A   127.0.0.1
    
    0 讨论(0)
  • 2020-12-07 15:39

    I realize that I'm pretty late responding to this question, but I had the same problem in regards to a local development solution. In another SO thread I found better solutions and thought I would share them for anyone with the same question in the future:

    VMware owned wild card domain that resolves any subdomain to 127.0.0.1:

    vcap.me resolves to 127.0.0.1
    www.vcap.me resolves to 127.0.0.1
    

    or for more versatility 37 Signals owns a domain to map any subdomain to any given IP using a specific format:

    127.0.0.1.xip.io resolves to 127.0.0.1
    www.127.0.0.1.xip.io resolves to 127.0.0.1
    db.192.168.0.1.xip.io resolves to 192.168.0.1
    

    see xip.io for more info

    0 讨论(0)
  • 2020-12-07 15:48

    First, I want to allow users to publish pages and give them each a subdomain of their choice (ex: user.mysite.com). From what I can gather, the best way to do this is to map user.mysite.com to mysite.com/user with mod_rewrite and .htaccess - is that correct?

    You may be better off using virtual hosts. That way, each user can have a webserver configuration pretty much independent of others.

    The syntax goes something like this:

    <VirtualHost *:80>
        DocumentRoot /var/www/user
        ServerName user.mysite.com
        ...
    </VirtualHost>
    0 讨论(0)
  • 2020-12-07 15:53

    From what I have seen on many webhosts, they setup a virtual host on apache.

    So if your www.mysite.com is served from /var/www, you could create a folder for each user. Then map the virtual host to that folder.

    With that, both mysite.com/user and user.mysite.com works.

    As for your test enviroment, if you are on windows, I would suggest editing your HOSTS file to map mysite.com to your local PC (127.0.0.1), as well as any subdomains you set up for testing.

    0 讨论(0)
  • 2020-12-07 15:54

    I am on Ubuntu 16.04 and since 14.04 I've using solution provided by Dave Evans here and it works fine for me.

    1. Install dnsmasq

      sudo apt-get install dnsmasq
      
    2. Create new file localhost.conf under /etc/dnsmasq.d dir with the following line

      #file /etc/dnsmasq.d/localhost.conf
      address=/localhost/127.0.0.1
      
    3. Edit /etc/dhcp/dhclient.conf and add the following line

      prepend domain-name-servers 127.0.0.1;
      

      (You’ll probably find that this line is already there and you just need to uncomment it.)

    4. Last one is restart the service

      sudo systemctl restart dnsmasq
      sudo dhclient
      

    Finally, you should check if it's working.

    dig whatever.localhost
    

    note:

    If you want to use it on your web server, you need to simply change the 127.0.0.0 to your actual IP address.

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