The background:
We've developed a crude content management system so that we can build a news section, a product reviews section, etc., integrating with our site. These "official" CMS sites should be available at, e.g., http://www.oursite.com/news. We also hope to offer "unofficial" sites to our community members; these will be available at, e.g., http://membername.oursite.com. We will ensure that there are no name clashes between official and unofficial ones.
The problem:
Here is an extract from .htaccess:
###################################################
# Redirect localhost.dev to www.localhost.dev #
###################################################
RewriteCond %{HTTP_HOST} ^localhost.dev$ [NC]
RewriteRule ^(.*)$ http://www.localhost.dev/$1 [R=301,L]
##################################################################
# UNOFFICIAL HOSTED sites - redirect mysite.localhost.dev URLs #
##################################################################
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.localhost\.dev$ [NC]
RewriteRule ^/?$ hosted/index.php?site=%1 [NC,QSA,L]
RewriteRule ^post/?$ hosted/posting.php?site=%1 [NC,QSA,L]
##########################################################################
# OFFICIAL HOSTED sites - redirect www.localhost.dev/officialblog URLs #
# Rationale: If /directory_name doesn't exist, maybe it's a hosted site. #
# Pass it to hosted site handler, which will 404 if not #
##########################################################################
RewriteCond %{HTTP_HOST} ^www.localhost.dev$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-z0-9_-]+)/?$ hosted/index.php?site=$1 [NC,QSA,L]
RewriteRule ^([a-zA-z0-9_-]+)/post/?$ hosted/posting.php?site=$1 [NC,QSA,L]
The first and the last sections give the expected behaviour. It's the "Unofficial hosted sites" part that is causing trouble.
http://unofficialblog.localhost.dev/ works - in hosted/index.php, $_GET['site'] is "unofficialblog".
http://unofficialblog.localhost.dev/post doesn't work - in hosted/posting.php, $_GET['site'] is "".
What I've tried
Swap the order of those two RewriteRules, and the opposite is true. So it seems that the %1 is valid only in the first one, not in the second.
If I do this:
##################################################################
# UNOFFICIAL HOSTED sites - redirect mysite.localhost.dev URLs #
##################################################################
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.localhost\.dev$ [NC]
RewriteRule ^/?$ hosted/index.php?site=%1 [NC,QSA,L]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.localhost\.dev$ [NC]
RewriteRule ^post/?$ hosted/posting.php?site=%1 [NC,QSA,L]
then $_GET['site'] is "unofficialblog" in both cases.
But that's hideously ugly, and I have about ten RewriteRules like this. Is there a cleaner way to keep the back-reference to the sub-domain across all of those RewriteRules?
EDIT: Looks like a duplicate of Multiple RewriteRules for single RewriteCond in .htaccess, which I didn't find when searching yesterday.