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

How do I say in an .htaccess file that I want to take anything that doesn't match:

main.php
index.php
/images/*
/css/*

and redirect it at the / of the current website?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Try this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/main.php$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/css/
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ / [R=301,L]
share|improve this answer
    
Anything else you need to turn on or enable to get this to work? Not working for me with just the .htaccess file and restarting server process –  Tyndall Jan 10 '12 at 18:20
    
mod_rewrite is being loaded. –  Tyndall Jan 10 '12 at 18:24
    
You may need to enable .htaccess files in the Apache config using AllowOverride –  xofer Jan 10 '12 at 18:33
    
For .htaccess files you also need the FollowSymlinks option. –  Ladadadada Jan 10 '12 at 21:14

Try this:

RewriteRule !(main.php|index.php|/images/|/css/) / [R]

You can "bookend" and of these with ^ and $ if you want them to be more restrictive.

RewriteRule !(main.php$|^/index.php$|^/images/|^/css/) / [R]

You will probably want to add ^/$ into that list as well or you will end up with a redirect loop.

share|improve this answer
1  
You need to include / in the list of items not to redirect or you'll get an infinite loop (which I discovered in testing my original answer). –  xofer Jan 10 '12 at 17:57
    
Thanks for the warning and alternate syntax. +1 –  Tyndall Jan 11 '12 at 14:54

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.