Frage

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.

War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top