title: Use mod_rewrite (.htaccess) to restrict URIs for different subdomains
body: <p>I have a website on an Apache server with multiple domains and subdomains.  I want to continue redirecting requests to most domains to the preferred domain, but now I also want to separate out a couple URIs to require use of new subdomains as described below.</p>
  
  <p>We have resources on the website under <code>/media</code>, and <code>/api</code> that I want to "move" off <code>www.domain.com</code> and onto <code>static.domain.com</code> and <code>api.domain.com</code>.  (The primary reason is to eliminate cookie traffic for static resources.)  The actual paths on the web server will not change.</p>
  
  <p>To complicate things, I want to continue to allow access to the primary domain to <code>/media</code>, but for anything under <code>/api</code>, which is brand new, I only want to allow access via <code>api.domain.com</code>.  Everything else should return a 404.</p>
  
  <p>EDIT:</p>
  
  <p>Based on Jon's answer below, I added the following block to restrict the <code>api</code> subdomain to URIs containing <code>/api/</code>.</p>
  
  <pre><code>#Restrict api subdomain to /api
  RewriteCond %{HTTP_HOST} ^api\. [NC]
  RewriteRule !^api/.*$ - [R=404,L]
  </code></pre>
  
  <p>However, I cannot seem to do the opposite and prevent all other subdomains from accessing the <code>/api/</code> URI.  The following block seems to have no effect.</p>
  
  <pre><code>#Disallow /api from any domain that doesn't start with 'api.'
  RewriteCond %{HTTP_HOST} !^api\. [NC]
  RewriteRule ^(api/.*)$ - [R=404,L]
  </code></pre>
  
  <p>Help is appreciated in getting this block fixed.</p>
  
  <p>EDIT 2:</p>
  
  <p>The current ModRewrite code is in .htaccess in the documentroot for the server.  There are no other .htaccess files and no ModRewrite config in httpd.conf.  I suspect my second issue above (disallowing /api) lies in the fact that everything is ultimately routed to CodeIgniter via index.php.</p>
  
  <pre><code>&lt;IfModule mod_rewrite.c&gt;
      RewriteEngine On
      RewriteBase /
  
      #For stats tracking:
      RewriteRule ^server-status - [L]
  
      #Send old canada URI requests to canada server:
      RewriteRule ^canada(.*)$ http://canada.domain.com$1 [R=301,L]
  
      #Don't allow access via server01 name:
      RewriteRule ^server01\.domain\.com(.*)$ http://www.domain.com$1 [R=301,L]
  
      #Restrict api subdomain to /api:
      RewriteCond %{HTTP_HOST} ^api\. [NC]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule !^api/.*$ - [R=404,L]
  
      #Make http://domain.com requests go to http://www.domain.com:
      RewriteCond %{HTTP_HOST} ^domain\.
      RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
      RewriteCond %{HTTP_HOST} domain\.us
      RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
      RewriteCond %{HTTP_HOST} domain\.net
      RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
  
      #Don't allow access by IP:
      RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX
      RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
  
      #Perform rewrite for CSS file versioning:
      RewriteCond %{REQUEST_URI} ^/style/([0-9]*)/(.*).css$
      RewriteRule ^style/.*/(.*)$ style/$1
  
      #Removes access to the system folder by users.
      #Additionally this will allow you to create a System.php controller,
      #previously this would not have been possible.
      #'system' can be replaced if you have renamed your system folder.
      RewriteCond %{REQUEST_URI} ^system.*
      RewriteRule ^(.*)$ /index.php?/$1 [L]
  
      #When your application folder isn't in the system folder
      #This snippet prevents user access to the application folder
      #Submitted by: Fabdrol
      #Rename 'application' to your applications folder name.
      RewriteCond %{REQUEST_URI} ^application.*
      RewriteRule ^(.*)$ /index.php?/$1 [L]
  
      #Checks to see if the user is attempting to access a valid file,
      #such as an image or css document, if this isn't true it sends the
      #request to index.php
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php?/$1 [L]
  &lt;/IfModule&gt;
  </code></pre>