Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.
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 mysite.com/one/two to go main->one(two)

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

how would I setup a rule like this for api.mysite.com to hit the following?

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

(allowing me to go api.mysite.com/one/two to hit api->one(two))

share|improve this question

2 Answers 2

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 api.mysite.com.. seems to not be working.. if I go api.mysite.com/api 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:

ServerName api.mysite.com
DocumentRoot /var/www/host/mysite.com/html
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.