0

I'm trying to do something incredibly simple: If the user requests the home page, http://www.OurSite.com/, then stop all rules and serve the file normally. If anything else in the world is requested, silently redirect to a php script.

I'm using this:

RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteRule .* /000_ROUTE.php [L]

It seems that my condition is being ignored and the home page is redirecting too.

How can I make this work?

Nick
  • 4,503
  • 29
  • 69
  • 97

1 Answers1

1

If the rule is used in a .htaccess file, use

RewriteCond %{REQUEST_URI} !^/000_ROUTE.php$
RewriteRule . /000_ROUTE.php [L]

If it's in your VirtualHost-block or main config file instead, use

RewriteCond %{REQUEST_URI} !^/000_ROUTE.php$
RewriteRule /. /000_ROUTE.php [L]

(the only difference is the leading slash on the RewriteRule).

The first thing Apache httpd does before evaluating the RewriteConds is to evaluate the RewriteRule. Putting the condition into the Rule (and in this case the condition is that there is at least one character after the slash) saves Apache having to evaluate the RewriteCond entirely for requests at "/". The RewriteCond ensures that you don't end up in a loop of rewrites -- after all, 000_ROUTE.php is not "/", either.)

Depending on how often that rule will get hit (i.e. if you expect it to get hit a lot and thus separating out a RewriteCond is just going to add overhead), you could also condense it into

RewriteRule ^(?!000_ROUTE.php). /000_ROUTE.php [R,L]

for .htaccess or -contexts

RewriteRule ^/(?!000_ROUTE.php). /000_ROUTE.php [R,L]

for or server-contexts.

eike
  • 176
  • 4