1

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?

2 Answers 2

1

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]
4
  • 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, 2012 at 18:20
  • mod_rewrite is being loaded.
    – Tyndall
    Jan 10, 2012 at 18:24
  • You may need to enable .htaccess files in the Apache config using AllowOverride
    – xofer
    Jan 10, 2012 at 18:33
  • For .htaccess files you also need the FollowSymlinks option.
    – Ladadadada
    Jan 10, 2012 at 21:14
1

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.

1
  • 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, 2012 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .