.htaccess redirect all urls except some and exclude my own IP

Hi,

I’m doing some redesign and maintenance of my site and I wanna put it in “maintenance mode” using .htaccess

But the actual “maintenance version” of the site got 3 pages…

So, I wanna redirect everything except those pages to the root of the domain and exclude my own IP from it…

…would something like this do? Or am I way off here? :rolleyes:


Options +FollowSymlinks
RewriteEngine on

#urls to exclude
RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
RewriteCond %{REQUEST_URI} !/url-to-exclude-2$

#ip to allow access
RewriteCond %{REMOTE_HOST} !^11\\.11\\.11\\.11

#send to root
RewriteRule $ /http://www.mydomain.com [R=302,L]

Thanks for any pointers!

Yes that looks pretty okay to me.

Two minor suggestions:

  1. Add a $ after the IP RewriteCond. For this example 11.11.11.11, 11.11.11.110, 11.11.11.111, 11.11.11.112, etc would all be valid, but I’m guessing you were going for just allowing 11.11.11.11

  2. Change the $ in the RewriteRule to .? which is slightly faster.

Otherwise the code looks good. And good call with R=302, too :tup:

First of all, thanks for your reply! :slight_smile: And yeah, only one IP…or several but then I just add another line as for the urls right?

So, this would do it?


Options +FollowSymlinks
RewriteEngine on

#urls to exclude
RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
RewriteCond %{REQUEST_URI} !/url-to-exclude-2$

#ip to allow access
RewriteCond %{REMOTE_HOST} !^11\\.11\\.11\\.11$

#send to root
RewriteRule .? /http://www.mydomain.com [R=302,L]

And I’ve seen when people are using an actual maintenance page they redirect to that they also exclude that one, to prevent an “infinite loop” someone wrote…is that necessary when doing like this…or will this also redirect www.mydomain.com to www.mydomain.com and so on and so on? …or is “apache smarter than that”!? :rolleyes:

Then I’ve also seen people exclude image/css files or folders containing those, is that also necessary and if so why?

And thanks again for taking the time! :slight_smile:

Yes :slight_smile:

Good point, you should include the / URL, otherwise you will indeed get an infinite loop!

Because otherwise the images and css files etc are also redirected to / since Apache doesn’t conscern between content type, it will just rewrite any URL you throw at it.

So you’d end up with something like


Options +FollowSymlinks
RewriteEngine on

#urls to exclude
RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
RewriteCond %{REQUEST_URI} !/url-to-exclude-2$
RewriteCond %{REQUEST_URI} !^/images$
RewriteCond %{REQUEST_URI} !^/css$
RewriteCond %{REQUEST_URI} !^/$

#ip to allow access
RewriteCond %{REMOTE_HOST} !^11\\.11\\.11\\.11$

#send to root
RewriteRule .? /http://www.mydomain.com [R=302,L]

:slight_smile:

Works like a charm! :wink:

Thanks a lot! :slight_smile:

Rémon,

Why did you allow the / before http: in the RewriteRule’s redirection? Apparently it’s being ignored but I expected it to cause a problem (it should).

Regards,

DK

I’m pretty sure that’s a slip of the fingers :blush: