views:

622

answers:

3

I want to redirect all user page requests to a page on the same domain.

For example, I have an "under construction, BRB" page that I want all users to see when they try to access ANY page on the site.

I tried using this:

Redirect 302 / http://www.domain.com/index2.php

What that does is try to apply the redirect to the index2.php page as well and it gets stuck in a loop where the user then sees this until the browser stops.

http://www.domain.com/index2.phpindex2.phpindex2.phpindex2.php etc., etc,

Any idea on how to write that rule to except that page?

A: 

You could use mod_rewrite

<IfModule mod_rewrite.c>
RewriteCond {REQUEST_URI} !=/index2.php
RewriteEngine on
RewriteRule ^.*$ /index2.php
# End .HTACCESS
</IfModule>

Eddy
That gives me a 500 internal server error.
Tom
sorry, forgot to exclude the file you want to serve. I've updated it.
Eddy
Thanks Eddy but I'm still getting an internal server error, any ideas as to why?
Tom
Hrm, could you add some logging eg) RewriteLogLevel 9 /path/to/logs
Eddy
A: 

I'd be more inclined to use the slightly different

Options +FollowSymLinks 
<IfModule mod_rewrite.c>
    RewriteEngine on 
    RewriteRule ^/.*$ /index2.php [R=307,L]
</IfModule>

This will let you return a moved temporarily status for the redirect.

Omitting this set of flags means that mod_rewrite will return a 302 Found status by default.

HTH

cheers,

Rob Wells
Trying that rule I get, "Too many redirects occurred" in my browser...
Tom
What is in your index2.php?
Rob Wells
And did you see my edit adding the / to the rewrite regexp?
Rob Wells
a few images and includes of header and footer information, could that be causing it? Meaning it's trying to redirect the include files?
Tom
Oh and an fyi, I've tried the code above and now it doesn't do any redirecting at all...
Tom
+2  A: 

You have to exclude the file you want to redirect to. Here’s an example with mod_rewrite:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule !^index2\.php$ /index2.php [L,R=302]
</IfModule>
Gumbo
I'm probably doing it wrong but my broswer is returning "Too many redirects" errors when I try this.
Tom
How would I write this so it wouldn't rewrite the CSS files required to display the page correctly?
Tom
No need to specify the 302 return, mod_rewrite will do that by default.
Rob Wells
@Tom: Add a RewriteCond directive to explude additional URI paths: RewriteCond %{REQUEST_URI} ^/(css|images|foobar)/`.
Gumbo
I think we're getting there but when I add that, the redirecting stops working....<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_URI} ^/(_assets)/ RewriteRule !^index2\.php$ /index2.php [L,R=302]</IfModule>
Tom
ok, changing your rewritecond to RewriteCond %{REQUEST_URI} !^/(_assets)/ works...(had to add the not qualifier before the carat)...thanks!
Tom