1

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.

Community
  • 1
  • 1
Ed Daniel
  • 502
  • 11
  • 21
  • possible duplicate of [Multiple RewriteRules for single RewriteCond in .htaccess](http://stackoverflow.com/questions/7218164/multiple-rewriterules-for-single-rewritecond-in-htaccess) – Ed Daniel May 06 '14 at 07:08

1 Answers1

1

Only the first rewrite rule after a set of rewite conditions is the one that set of conditions is 'related' to. Here's the meta breakdown of how conditions work:

  RewriteCond a1 (can evaluate  references from rule a)
  RewriteCond a2 (can evaluate  references from rule a)
  RewriteRule a <--the conditions a1, a2 apply only to things matching this rule
  RewriteRule b <--the conditions a1, a2 are not applicable to this rule

To make them work on both rules you'd need to do this:

  RewriteCond a1 (can evaluate  references from rule a)
  RewriteCond a2 (can evaluate  references from rule a)
  RewriteRule a

  RewriteCond copy of a1 (can evaluate  references from rule b)
  RewriteCond copy of a2 (can evaluate  references from rule b)
  RewriteRule b
Ray
  • 36,097
  • 17
  • 85
  • 129
  • So it's sheer blind luck that I get the right results in the "official" section... Now I test some more, I see that that's indeed the case. There's no cleaner way than this, though? – Ed Daniel May 05 '14 at 19:45
  • @EdDaniel yep, I'm trying to see why the (last noted) unofficial version isn't passing the parameters for subsitution. – Ray May 05 '14 at 19:47
  • @EdDaniel oh, you might try to put the new full url at the front, not the relative one. – Ray May 05 '14 at 19:49
  • @EdDaniel Doh... The condition is failing on the second rule in your final example. You're second condition matching regex string shouldn't end a `.dev$` but end with `.dev/post$` – Ray May 05 '14 at 19:53
  • @EdDaniel see my update to the answer explicitly showing the mismatch condition and rule – Ray May 05 '14 at 19:59
  • @EdDaniel oops, scratch that my mistake – Ray May 05 '14 at 20:00