Wildcard Subdomains

后端 未结 8 898
广开言路
广开言路 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:58

    The best thing to do if you are running *AMP is to do what Thomas suggests and do virtual hosts in Apache. You can do this either with or without the redirect you describe.

    Virtual hosts

    Most likely you will want to do name-based virtual hosts, as it's easiest to set up and only requires one IP address (so will also be easy to set up and test on your local MAMP machine). IP-based virtual hosts is better in some other respects, but you have to have an IP address for each domain.

    This Wikipedia page discusses the differences and links to a good basic walk-thru of how to do name-based vhosts at the bottom.

    On your local machine for testing, you'll also have to set up fake DNS names in /etc/hosts for your fake test domain names. i.e. if you have Apache listening on localhost and set up vhost1.test.domain and vhost2.test.domain in your Apache configs, you'd just add these domains to the 127.0.0.1 line in /etc/hosts, after localhost:

    127.0.0.1 localhost vhost1.test.domain vhost2.test.domain
    

    Once you've done the /etc/hosts edit and added the name-based virtual host configs to your Apache configuration file(s), that's it, restart Apache and your test domains should work.

    Redirect with mod_rewrite

    If you want to do redirects with mod_rewrite (so that user.example.com isn't directly hosted and instead redirects to example.com/user), then you will also need to do a RewriteCond to match the subdomain and redirect it:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
    RewriteRule ^(.*)$ http://example.com/subdomain$1 [R]
    

    You can put this in a .htaccess or in your main Apache config.

    You will need to add a pair of rules like the last two for each subdomain you want to redirect. Or, you may be able to capture the subdomain in a RewriteCond to be able to use one wildcard rule to redirect *.example.com to example.com/ * -- but that smells really bad to me from a security standpoint.

    All together, vhosts and redirect

    It's better to be more explicit and set up a virtual host configuration section for each hostname you want to listen for, and put the rewrite rules for each of these hostnames inside its virtual host config. (It is always more secure and faster to put this kind of stuff inside your Apache config and not .htaccess, if you can help it -- .htaccess slows performance because Apache is constantly scouring the filesystem for .htaccess files and reparsing them, and it's less secure because these can be screwed up by users.)

    All together like that, the vhost config inside your Apache configs would be:

    NameVirtualHost 127.0.0.1:80
    
    # Your "default" configuration must go first
    <VirtualHost 127.0.0.1:80>
      ServerName example.com
      ServerAlias www.example.com
      DocumentRoot /www/siteroot
      # etc.
    </VirtualHost>
    
    # First subdomain you want to redirect
    <VirtualHost 127.0.0.1:80>
      ServerName vhost1.example.com
      RewriteEngine On
      RewriteRule ^(.*)$ http://example.com/vhost1$1 [R]
    </VirtualHost>
    
    # Second subdomain you want to redirect
    <VirtualHost 127.0.0.1:80>
      ServerName vhost2.example.com
      RewriteEngine On
      RewriteRule ^(.*)$ http://example.com/vhost2$1 [R]
    </VirtualHost>
    
    0 讨论(0)
  • 2020-12-07 16:00

    I had to do exactly the same for one of my sites. You can follow the following steps

    1. If you've cPanel on your server, create a subdomain *, if not, you'd have to set-up an A record in your DNS (for BIND see http://ma.tt/2003/10/wildcard-dns-and-sub-domains/). On your dev. server you'd be far better off faking subdomains by adding each to your hosts file.

    2. (If you used cPanel you won't have to do this). You'll have to add soemthing like the following to your apache vhosts file. It largely depends on what type of server (shared or not) you're running. THE FOLLOWING CODE IS NOT COMPLETE. IT'S JUST TO GIVE DIRECTION. NOTE: ServerAlias example.com *.example.com is important.

      <VirtualHost 127.0.0.1:80>  
              DocumentRoot /var/www/  
              ServerName example.com  
              ServerAlias example.com *.example.com  
      </VirtualHost>
      
    3. Next, since you can use the PHP script to check the "Host" header and find out the subdomain and serve content accordingly.

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