Apache mod_rewrite ANY subdomain(s) to root domain, unless existent as virtualdocumentroot


Say you've got an Apache2 virtual host setup, something like this:

/htdocs/parent1.com /htdocs/sub1.parent1.com /htdocs/sub2.parent1.com /htdocs/parent2.net /htdocs/parentn.org

Say you'd like to do this with VirtualDocumentRoot /htdocs/%0, so that you can add and remove virtual hosts without tinkering with your Apache configuration. That's important: please please no messing with htaccess files or httpd.conf every time a virtual host comes or goes - whether that host is a parent domain or not. In fact, say you're using AllowOverride None.

Anyway, the question is, how might you 301 redirect non-existent sub-domains to their corresponding parent domains without redirecting existent sub-domains?


I may have solved my own problem. However I would appreciate any feedback if somebody finds a problem with what I'm doing.

The following leaves alone any request to an arbitrary subdomain, as long as there exists a corresponding document root; but redirects any request to a subdomain which does not exist in the filesystem.

<IfModule rewrite_module> RewriteEngine On RewriteMap lowercase int:tolower RewriteCond "/htdocs/${lowercase:%{HTTP_HOST}}" !-d RewriteCond %{HTTP_HOST} "\.([^\.]+\.[^\.]+)$" RewriteRule ^/(.*)$ "http://%1/$1" [R=301] </IfModule>

Allows me to setup wildcard DNS and use name-based virtual hosting, without touching any configuration settings. Also, there's no htaccess involved. Just make your folder with any name like "/htdocs/[host.]domain.tld" and you're up and running. As far as I can tell, this doesn't really work with SSL/TLS (presumably something to do with %{HTTP_HOST}?), but secure sites are comparably few and better resolved by IP address than by hostname.


  • Invalid controller specified() - Zend Framework
  • Does cache() in spark change the state of the RDD or create a new one?
  • Application of DirectoryIndex to an Alias in Apache
  • Is there a way to remove '/web' from url in Symfony application without moving content of
  • Wamp, SlimPHP and Htaccess
  • (Un)setting a cookie in mod_rewrite
  • deploying Node.js app for production
  • Node can't scrape certain pages
  • Help with mod_rewrite
  • https in htaccess and order of rules (using Expression Engine)
  • Removing the .php file extension from the URL Request
  • Version in docker-compose is unsupported
  • how to bypass pinentry (passphrase screen) while decrypting a file using gpgme
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • python script hangs on input method when running spark
  • Force show.bind execution
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • htaccess add www if not subdomain, if subdomain remove www
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Email verification using google app script and google forms
  • Disable Enter in editText android
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • What is the “return” in scheme?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Arrays break string types in Julia
  • Is there a mandatory requirement to switch app.yaml?
  • How do you join a server to an Active Directory (domain)?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key