Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

Would anybody know how to write an .htaccess file that will redirect ALL traffic for ALL requests to index.php except for two files.

And in this process, also redirect any request without "www" to "www"

And also, can I make the .htaccess in the root directory the only main one to be used, followed, read, etc. So I don't have to put .htaccess files in every sub folder.

And also, should I put [NC] at the ends of these directives?


DirectoryIndex index.php

RewriteEngine on
RewriteCond %{HTTP_HOST} ^$
RewriteCond %{REQUEST_URI} ! ^/robots\.txt$
RewriteCond %{REQUEST_URI} ! ^/googlestuff\.html$
RewriteRule ^/?$ "http\:\/\/www\.example\.com\/" [R=301,L]

but it didn't work???

share|improve this question

Make sure there is no 'AllowOverride None' in the webserver config for your virtualhost. Try to set 'AllowOverride All'.

For example in the debian, webserver is configured by default with 'AllowOverride None' option, which not allows to override settings using .htaccess.

Also, you don't need to put .htaccess in subdirectories when settings in these directories remain same as in parent folder.

share|improve this answer

You're ! needs to be right before the regex otherwise apache mistakes it as its own parameter and assumes it's the pattern. Your pattern in the rule itself needs to be generic enough to match everything.

DirectoryIndex index.php

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/robots\.txt$
RewriteCond %{REQUEST_URI} !^/googlestuff\.html$
RewriteRule ^/?(.+) [R=301,L]

If you want to force requests to have a www in the hostname, you have to do that separately, and this is where you'd want to use the NC flag:

RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]
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.