Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to setup several rewrite rules that allow a restful url structure to exist, but only on a certain subdomain. Several subdomains already point to the shared codebase. I also have an existing rewrite in place in order to use PHP smarty, which already captures everything. All subdomains including the restful one need to redirect there as a final step.

Finally, for convenience I'd like to try to put the subdomain into a query string variable if possible as well, to avoid trying to extract it programmatically later.

I'm new to mod_rewrite and am having trouble getting all these to work together... here is my mixed attempts so far, which doesn't quite work all together.

# copy subdomain to variable.  
# ideally though, would only copy "subdomain", not "dev.subdomain". 
# this should work for all subdomain, and keep going.
RewriteCond %{HTTP_HOST} (.+) [NC]
RewriteRule / /$1?subdomain=%1 [QSA]

# restful api, only for how to apply to all rules?
RewriteCond %{HTTP_HOST}
RewriteRule ^/user/([^/]+)/ /calendar/?id=$1 [QSA,N]
RewriteRule ^/admin/([^/]+)/ /calendar_admin/?id=$1 [QSA,N]
RewriteRule ^/user/([^/]+)/calendar/ /calendar/?id=$1 [QSA,N]
RewriteRule ^/user/([^/]+)/profile/ /profile/?id=$1 [QSA,N]

# Smarty rewrite rule the request from "/foo/bar/" to "/dispatch.php/foo/bar/"
RewriteRule     ^(.*)$    dispatch.php/$1 [L]

How can I make the first section only apply for the subdomain? Can it still rewrite, then do the smarty rule with the new urls?

share|improve this question
Play around using this: which helps debugging and read… – ETL Mar 9 '14 at 22:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.