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

I recently had some help from with a htaccess rewrite that sends all subdomain requests to

You can find the thread here and the code is here:

RewriteBase /
RewriteEngine On

#### URL Rewrite Handler for Subdomains (by Randall Krause) ####

RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/%1 -d
RewriteRule ^(.*) apps/%1/$1 [E=SUBDOMAIN:%1,L]

As I said this works really well, but I need to add two more rules that I am having trouble integrating without conflicting with the above code.

  1. For any requests that are NOT for subdomains I need the main website URL ( to rewrite to http: //
  2. For any requests that are NOT for the subdomains I also need to rewrite the URLS to remove an index.php from the URL.

The code for this is:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Adding this in its current form BEFORE the subdomain rewrite rule breaks it.

The only solution I have found is to explicitly define which files are not to be covered by the above rule by doing something like this:

RewriteCond $1 !^(index\.php|images|robots\.txt)

Does anyone have any ideas?



share|improve this question
up vote 0 down vote accepted

Haven't tested it, but try something along these lines (you might need to tweak it):

RewriteCond %{HTTP_HOST} ^domain\.com(:80)?$ [NC]
RewriteRule ^(.*)(?:index.php)?$$1 [R=permanent,L]

Put the code after RewriteEngine On (i.e. before your first RewriteCond). Note that it's going to remove all index.php endings (i.e. will become Taken from here:

I suggest you do some reading on mod_rewrite, such as:

if you are going to need it every second day - pays off... :)

share|improve this answer
Hi, Thanks for replying and appologies for bothering you yet again. The www. redirect works without conflicting (Thanks), but it is not removing the index.php. I have been looking through the links you posted but they give me a case of information overload. Thanks for all your help. I will keep searching! Tim – Tim Nov 24 '10 at 9:58
Also, I took out (?:index.php) and nothing changed. Does anyone have any information on this??? – Tim Nov 24 '10 at 10:34
Try this then: RewriteRule ^(.*?)(?:/index.php)?$$1 [R=permanent,L]. – Nov 25 '10 at 0:16
No sorry. I don't know why it isnt't working. I don't know if it's because I am using codeigniter but I have set all the config options to the right url. – Tim Nov 25 '10 at 4:48
Do you have your RewriteLog turned on? See here: and this SOq:…. This will allow you to see what is happening - you may want to post that back to your question. – Nov 25 '10 at 14:12

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.