<p>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...</p>
<p>With that in mind, here is what I'm trying to do:</p>
<p>First, I want to allow users to publish pages and give them each a subdomain of their choice (ex: <code>user.example.com</code>). From what I can gather, the best way to do this is to map <code>user.example.com</code> to <code>example.com/user</code> with mod_rewrite and <code>.htaccess</code> - is that correct?</p>
<p>If that is correct, can somebody give me explicit instructions on how to do this?</p>
<p>Also, I am doing all of my development locally, using MAMP, so if somebody could tell me how to set up my local environment to work in the same manner (I've read this is more difficult), I would greatly appreciate it. Honestly, I have been trying a everything to no avail, and since this is my first time doing something like this, I am completely lost.</p>
<p><em>Some of these answers have been REALLY helpful, but for the system I have in mind, manually adding a subdomain for each user is not an option. What I'm really asking is how to do this on the fly, and redirect <code>wildcard.example.com</code> to <code>example.com/wildcard</code> -- the way Tumblr is set up is a perfect example of what I'd like to do.</em></p>

[{'answer_id': 79807, 'author': 'Thomas', 'author_id': 14637, 'author_profile': 'https://Stackoverflow.com/users/14637', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>First, I want to allow users to\n  publish pages and give them each a\n  subdomain of their choice (ex:\n  user.mysite.com). From what I can\n  gather, the best way to do this is to\n  map user.mysite.com to mysite.com/user\n  with mod_rewrite and .htaccess - is\n  that correct?</p>\n</blockquote>\n\n<p>You may be better off using <a href="http://httpd.apache.org/docs/1.3/vhosts/" rel="nofollow noreferrer">virtual hosts</a>. That way, each user can have a webserver configuration pretty much independent of others.</p>\n\n<p>The syntax goes something like this:</p>\n\n<pre>&lt;VirtualHost *:80&gt;\n    DocumentRoot /var/www/user\n    ServerName user.mysite.com\n    ...\n&lt;/VirtualHost&gt;</pre>\n'}, {'answer_id': 79811, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 5, 'selected': True, 'text': "<p>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.</p>\n<p>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 preferred route if possible). You could also handle rewriting at the application level as well (like using routing if on ASP.NET).</p>\n<p>You'd rewrite the URL so <code>http://myname.example.com</code> would become <code>http://example.com/something.aspx?name=myname</code> 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.</p>\n<p>I am not suggesting that you create a subdomain for each user, but instead create a wildcard subdomain for the domain itself, so <em>anything</em>.example.com (basically <code>*.example.com</code>) goes to your site. I have several domains setup with MyDomain. Their instructions for setting this up is like this:</p>\n<blockquote>\n<p>Yes, you can configure a wild card but\nit will only work if you set it up as\nan A Record. Wildcards do not work\nwith a C Name. To use a wildcard, you\nuse the asterisks character '<em>'. For\nexample, if you create and A Record\nusing a wild card, <code>*.example.com</code>,\nanything that is entered in the place\nwhere the '</em>' is located, will resolve\nto the specified IP address. So if you\nenter 'www', 'ftp', 'site', or\nanything else before the domain name,\nit will always resolve to the IP\naddress</p>\n</blockquote>\n<p>I have some that are setup in just this way, having <code>*.example.com</code> go to my site. I then can read the base URL in my web app to see that <code>ryan.example.com</code> is what was currently accessed, or that <code>bill.example.com</code> is what was used. I can then either:</p>\n<ol>\n<li>Use URL rewriting so that the subdomain becomes a part of the query string OR</li>\n<li>Simply read the host value from the accessed URL and perform some logic based on that value.</li>\n</ol>\n<p>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)</p>\n<p><strong>Edit 2:</strong></p>\n<p>There is no way to do this without a DNS entry. The &quot;online world&quot; needs to know that <code>name1.example.com</code>, <code>name2.example.com</code>,..., <code>nameN.example.com</code> 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.</p>\n"}, {'answer_id': 79885, 'author': 'Scott Swezey', 'author_id': 9439, 'author_profile': 'https://Stackoverflow.com/users/9439', 'pm_score': 0, 'selected': False, 'text': '<p>From what I have seen on many webhosts, they setup a virtual host on apache.</p>\n\n<p>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.</p>\n\n<p>With that, both mysite.com/user and user.mysite.com works.</p>\n\n<p>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.</p>\n'}, {'answer_id': 80122, 'author': 'joelhardi', 'author_id': 11438, 'author_profile': 'https://Stackoverflow.com/users/11438', 'pm_score': 4, 'selected': False, 'text': '<p>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.</p>\n<h2>Virtual hosts</h2>\n<p>Most likely you will want to do <a href="http://httpd.apache.org/docs/2.0/vhosts/examples.html" rel="nofollow noreferrer">name-based virtual hosts</a>, 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.</p>\n<p>This <a href="http://en.wikipedia.org/wiki/Virtual_hosting" rel="nofollow noreferrer">Wikipedia page</a> discusses the differences and links to a good basic walk-thru of how to do name-based vhosts at the bottom.</p>\n<p>On your local machine for testing, you\'ll also have to set up fake DNS names in <code>/etc/hosts</code> 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 <code>127.0.0.1</code> line in <code>/etc/hosts</code>, after localhost:</p>\n<pre><code>127.0.0.1 localhost vhost1.test.domain vhost2.test.domain\n</code></pre>\n<p>Once you\'ve done the <code>/etc/hosts</code> 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.</p>\n<h2>Redirect with mod_rewrite</h2>\n<p>If you want to do redirects with mod_rewrite (so that <code>user.example.com</code> isn\'t directly hosted and instead redirects to <code>example.com/user</code>), then you will also need to do a RewriteCond to match the subdomain and redirect it:</p>\n<pre><code>RewriteEngine On\nRewriteCond %{HTTP_HOST} ^subdomain\\.example\\.com\nRewriteRule ^(.*)$ http://example.com/subdomain$1 [R]\n</code></pre>\n<p>You can put this in a <code>.htaccess</code> or in your main Apache config.</p>\n<p>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 <code>example.com/</code></p>\n<ul>\n<li>-- but that smells really bad to me from a security standpoint.</li>\n</ul>\n<h2>All together, vhosts and redirect</h2>\n<p>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 <code>.htaccess</code>, if you can help it -- <code>.htaccess</code> slows performance because Apache is constantly scouring the filesystem for <code>.htaccess</code> files and reparsing them, and it\'s less secure because these can be screwed up by users.)</p>\n<p>All together like that, the vhost config inside your Apache configs would be:</p>\n<pre><code>NameVirtualHost 127.0.0.1:80\n\n# Your &quot;default&quot; configuration must go first\n&lt;VirtualHost 127.0.0.1:80&gt;\n  ServerName example.com\n  ServerAlias www.example.com\n  DocumentRoot /www/siteroot\n  # etc.\n&lt;/VirtualHost&gt;\n\n# First subdomain you want to redirect\n&lt;VirtualHost 127.0.0.1:80&gt;\n  ServerName vhost1.example.com\n  RewriteEngine On\n  RewriteRule ^(.*)$ http://example.com/vhost1$1 [R]\n&lt;/VirtualHost&gt;\n\n# Second subdomain you want to redirect\n&lt;VirtualHost 127.0.0.1:80&gt;\n  ServerName vhost2.example.com\n  RewriteEngine On\n  RewriteRule ^(.*)$ http://example.com/vhost2$1 [R]\n&lt;/VirtualHost&gt;\n</code></pre>\n'}, {'answer_id': 3381158, 'author': 'Arvind Gupta', 'author_id': 407820, 'author_profile': 'https://Stackoverflow.com/users/407820', 'pm_score': 1, 'selected': False, 'text': '<p>I had to do exactly the same for one of my sites. You can follow the following steps</p>\n\n<ol>\n<li><p>If you\'ve cPanel on your server, create a subdomain <code>*</code>, if not, you\'d have to set-up an A record in your DNS (for BIND see <a href="http://ma.tt/2003/10/wildcard-dns-and-sub-domains/" rel="nofollow noreferrer">http://ma.tt/2003/10/wildcard-dns-and-sub-domains/</a>). On your dev. server you\'d be far better off faking subdomains by adding each to your <code>hosts</code> file.</p></li>\n<li><p>(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: <code>ServerAlias example.com *.example.com</code> is important.  </p>\n\n<pre><code>&lt;VirtualHost 127.0.0.1:80&gt;  \n        DocumentRoot /var/www/  \n        ServerName example.com  \n        ServerAlias example.com *.example.com  \n&lt;/VirtualHost&gt;\n</code></pre></li>\n<li><p>Next, since you can use the PHP script to check the "Host" header and find out the subdomain and serve content accordingly.</p></li>\n</ol>\n'}, {'answer_id': 13572237, 'author': 'Beau', 'author_id': 325758, 'author_profile': 'https://Stackoverflow.com/users/325758', 'pm_score': 3, 'selected': False, 'text': '<p>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 <a href="https://stackoverflow.com/questions/1562954/public-wildcard-domain-name-to-resolve-to-127-0-0-1">another SO thread</a> I found better solutions and thought I would share them for anyone with the same question in the future:</p>\n\n<p>VMware owned wild card domain that resolves any subdomain to 127.0.0.1:</p>\n\n<pre><code>vcap.me resolves to 127.0.0.1\nwww.vcap.me resolves to 127.0.0.1\n</code></pre>\n\n<p>or for more versatility 37 Signals owns a domain to map any subdomain to any given IP using a specific format:</p>\n\n<pre><code>127.0.0.1.xip.io resolves to 127.0.0.1\nwww.127.0.0.1.xip.io resolves to 127.0.0.1\ndb.192.168.0.1.xip.io resolves to 192.168.0.1\n</code></pre>\n\n<p>see <a href="http://xip.io/" rel="nofollow noreferrer">xip.io</a> for more info</p>\n'}, {'answer_id': 40946135, 'author': 'Fery W', 'author_id': 881743, 'author_profile': 'https://Stackoverflow.com/users/881743', 'pm_score': 2, 'selected': False, 'text': '<p>I am on Ubuntu 16.04 and since 14.04 I\'ve using solution provided by Dave Evans <a href="http://evans.io/legacy/posts/wildcard-subdomains-of-localhost/" rel="nofollow noreferrer">here</a> and it works fine for me.</p>\n\n<ol>\n<li><p>Install <code>dnsmasq</code></p>\n\n<pre><code>sudo apt-get install dnsmasq\n</code></pre></li>\n<li><p>Create new file <code>localhost.conf</code> under <code>/etc/dnsmasq.d</code> dir with the following line</p>\n\n<pre><code>#file /etc/dnsmasq.d/localhost.conf\naddress=/localhost/127.0.0.1\n</code></pre></li>\n<li><p>Edit <code>/etc/dhcp/dhclient.conf</code> and add the following line</p>\n\n<pre><code>prepend domain-name-servers 127.0.0.1;\n</code></pre>\n\n<p><em>(You’ll probably find that this line is already there and you just need to uncomment it.)</em></p></li>\n<li><p>Last one is restart the service</p>\n\n<pre><code>sudo systemctl restart dnsmasq\nsudo dhclient\n</code></pre></li>\n</ol>\n\n<p>Finally, you should check if it\'s working.</p>\n\n<pre><code>dig whatever.localhost\n</code></pre>\n\n<p><strong>note:</strong></p>\n\n<p>If you want to use it on your web server, you need to simply change the <code>127.0.0.0</code> to your actual IP address. </p>\n'}, {'answer_id': 51949318, 'author': 'Nicolay77', 'author_id': 82782, 'author_profile': 'https://Stackoverflow.com/users/82782', 'pm_score': 0, 'selected': False, 'text': '<p>The solution I found for Ubuntu 18.04 is similar to <a href="https://stackoverflow.com/a/40946135/82782">this one</a> but involves NetworkManager config:</p>\n\n<ol>\n<li><p>Edit the file <code>/etc/NetworkManager/NetworkManager.conf</code>, and add the line <code>dns=dnsmasq</code> to the <code>[main]</code> section</p>\n\n<pre><code>sudo editor /etc/NetworkManager/NetworkManager.conf\n</code></pre>\n\n<p>should look like this:</p>\n\n<pre><code>[main]\nplugins=ifupdown,keyfile\ndns=dnsmasq\n...\n</code></pre></li>\n<li><p>Start using NetworkManager\'s resolv.conf</p>\n\n<pre><code>sudo rm /etc/resolv.conf\nsudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf\n</code></pre></li>\n<li><p>Create a file with your wildcard configuration</p>\n\n<pre><code>echo \'address=/.localhost/127.0.0.1\' | sudo tee /etc/NetworkManager/dnsmasq.d/localhost-wildcard.conf\n</code></pre></li>\n<li><p>Reload NetworkManager configuration</p>\n\n<pre><code>sudo systemctl reload NetworkManager\n</code></pre></li>\n<li><p>Test it</p>\n\n<pre><code>dig localdomain.localhost\n</code></pre></li>\n</ol>\n\n<p>You can also add any other domain, quite useful for some types of authentication when using a local development setup.</p>\n\n<pre><code>echo \'address=/.local-dev.workdomain.com/127.0.0.1\' | sudo tee /etc/NetworkManager/dnsmasq.d/workdomain-wildcard.conf\n</code></pre>\n\n<p>Then this works:</p>\n\n<pre><code>dig petproject.local-dev.workdomain.com\n\n;; ANSWER SECTION:\npetproject.local-dev.workdomain.com. 0 IN   A   127.0.0.1\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/79764', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14891/']