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
RewriteEngine on
Options -Indexes
DirectoryIndex index.php
RewriteBase /

RewriteRule ^user/([a-zA-Z]+)/([a-zA-Z]+)(/)?$ index.php?controller=main&function=$1&arguments=$2 [NC,L]

I can go to go main->one(two)

I'd like to route subdomain requests the same way.

how would I setup a rule like this for to hit the following?

controller=api&function=$1&arguments=$2 [NC,L]

(allowing me to go to hit api->one(two))

share|improve this question
up vote 3 down vote accepted

It would be something like

RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com
RewriteRule ^user/([a-zA-Z]+)/([a-zA-Z]+)(/)?$  \
  index.php?controller=%1&function=$1&arguments=$2 [NC,L]

mod_rewrite allows the use of %N, where N is 1..9, to match bracketed patterns from the previous RewriteCond, so %1 is used to replace in the api domain prefix from the rewriteCond

share|improve this answer
trying: RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com and RewriteRule ^(/)?$ index.php?controller=%1&function=index [NC,L] to get the root of seems to not be working.. if I go it will work though.. when i do RewriteRule ^api(/)?$ index.php?controller=%1&function=index [NC,L] – tester May 30 '12 at 1:04
Nevermind. Ended up being an ordering issue – tester May 30 '12 at 1:18

I did end up finding that it was just easier to make a new virtual host and point it at a new, api-specific htaccess file:

DocumentRoot /var/www/host/
AccessFileName .api.htaccess # <-- this did the trick
share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.