Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

Tried:

DirectoryIndex index.php

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
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
add comment

2 Answers 2

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
add comment

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 ^/?(.+) http://www.example.com/ [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} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
share|improve this answer
add comment

Your Answer

 
discard

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.