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))


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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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