Search code examples
.htaccessexceptionsubdomain

htaccess exception for subdomain


I have this line in my htaccess file: RewriteRule (.*) index.php This catches all requests being sent to my site domain.com Now i have created a subdomain blog.domain.com and i am getting an Internal server error. How can i add blog.domain.com as an exception in htaccess.

This is my whole htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} .*/http-bind
RewriteRule (.*) /http-bind [L]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.raw|/[^.]*)$  [NC]
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} !/google3ce18567a119af4.html
RewriteRule (.*) index.php

Solution

  • Add the following line to the top of your RewriteCond block.

    RewriteCond %{HTTP_HOST} !^blog\.domain\.com
    RewriteCond %{REQUEST_URI} !^/index.php
    RewriteCond %{REQUEST_URI} !/ow_updates/index.php
    RewriteCond %{REQUEST_URI} !/ow_updates/
    RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.raw|/[^.]*)$  [NC]
    RewriteCond %{REQUEST_URI} !/ow_updates/
    RewriteCond %{REQUEST_URI} !/google3ce18567a119af4.html
    

    This will only pass when the request is not for blog.domain.com and meets all of your current conditions.

    As a side, I would encourage you to do something more like the following as opposed to your specific file checks.

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    

    This ensures that the request is not an existing file or directory. It's easier to maintain than whitelisting specific resources (e.g. google3ce18567a119af4.html, /ow_updates/, etc).