2

I am trying to shutdown a website to everyone except my ip address. I almost have it working. I cannot access www.mysite.com, but I can access all folders that have another .htaccess file in them.

I have a .htaccess file in /www with the following code:

#Use this when website is down
RewriteEngine on

#this allows access through my ip
RewriteCond %{REMOTE_ADDR}  !^(66\.777\.888\.99)$
RewriteRule  !down.php$ /down.php [L]

Some folders in my site have .htaccess files in them. If I have a file with the line:

RewriteEngine on

I can still access the folder. For example, if I have the second .htaccess file in /www/about, then I can still access mysite.com/about (but the .css file included on that page actually loads down.php).

If I delete "RewriteEngine on" I get redirected to down.php.

Any ideas? I think my mod_rewrite gets confused with multiple .htaccess files.

Thanks!

6 Answers 6

4

how about putting:

Order deny,allow
Deny from all
Allow from ip.ad.dre.ss

in your .htaccess file? or even in the system wide httpd configuration?

1
  • because then you don't get a custom "down" message (unless you also change the 403 error message) May 28, 2010 at 8:51
2

If you can reach your (virtual)host config, why not add these lines?

<Location />
    Order deny,allow
    Deny from all
    Allow from 1.1.1.1
    ErrorDocument 403 /down.php
</Location>
<Location /down.php>
    Order allow,deny
    Allow from all
</Location>
1
  • This (or almost identical to this) is what i use to lock down our production environments for updates.
    – grufftech
    Jun 7, 2010 at 22:53
1

Check your apache log file for errors, it's possible you'll be seeing a 404 somewhere for about/down.php. Otherwise, default Mod Rewrite debugging tips follow:

Try adding this to your http config (Somewhere in the config, it doesn't work at htaccess or virtualhost level)

RewriteLog /tmp/rewrite.log
RewriteLogLevel 9

This will provide you with a line by line explaination of what it's trying to match against what regex, and what the final decision is.

Remove these lines afterwards, otherwise one day you'll discover you don't have nearly as much disk space as you thought you should...

0

Thanks for the tip about RewriteLog. I tested it out, and it looks like the .htaccess file in the site root only applies to directories that do not have their own .htaccess file with the line "RewriteEngine on".

note, 12.345.678.90 is my ip, and 99.888... is the ip I want to allow to see stuff.

12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b878d060/initial] (1) [perdir /srv/www/mysite.com/about/] pass through 

/srv/www/mysite.com/about/
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b879b090/subreq] (1) [perdir /srv/www/mysite.com/about/] pass through 

/srv/www/mysite.com/about/index.html
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b879b090/subreq] (1) [perdir /srv/www/mysite.com/about/] pass through 

/srv/www/mysite.com/about/index.cgi
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b879b090/subreq] (1) [perdir /srv/www/mysite.com/about/] pass through 

/srv/www/mysite.com/about/index.pl
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b879b090/subreq] (1) [perdir /srv/www/mysite.com/about/] pass through 

/srv/www/mysite.com/about/index.php
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8791070/initial] (1) [perdir /srv/www/mysite.com/about/] pass through 

/srv/www/mysite.com/about/css/about.css

12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b87df100/initial] (3) [perdir /srv/www/mysite.com/] strip per-dir prefix: 

/srv/www/mysite.com/requires/css/header.css -> requires/css/header.css
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b87df100/initial] (3) [perdir /srv/www/mysite.com/] applying pattern 

'down.php$' to uri 'requires/css/header.css'
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b87df100/initial] (4) [perdir /srv/www/mysite.com/] RewriteCond: 

input='12.345.678.90' pattern='!^(99\.888\.777\.88)$' => matched
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b87df100/initial] (2) [perdir /srv/www/mysite.com/] rewrite 

'requires/css/header.css' -> '/down.php'
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b87df100/initial] (1) [perdir /srv/www/mysite.com/] internal redirect with 

/down.php [INTERNAL REDIRECT]
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8790810/initial/redir#1] (3) [perdir /srv/www/mysite.com/] strip per-dir 

prefix: /srv/www/mysite.com/down.php -> down.php
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8790810/initial/redir#1] (3) [perdir /srv/www/mysite.com/] applying pattern 

'down.php$' to uri 'down.php'
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8790810/initial/redir#1] (3) [perdir /srv/www/mysite.com/] strip per-dir 

prefix: /srv/www/mysite.com/down.php -> down.php

12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8790810/initial/redir#1] (3) [perdir /srv/www/mysite.com/] applying pattern 

'^(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])-((19|20)\d\d)/?(/([a-zA-Z0-9_-]{1,20}))?$' to uri 'down.php'
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8790810/initial/redir#1] (1) [perdir /srv/www/mysite.com/] pass through 

/srv/www/mysite.com/down.php
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b879d098/initial] (3) [perdir /srv/www/mysite.com/login/] strip per-dir 

prefix: /srv/www/mysite.com/login/js/ajax_login.js -> js/ajax_login.js
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b879d098/initial] (3) [perdir /srv/www/mysite.com/login/] applying pattern 

'^([a-z0-9]+/?){0,3}$' to uri 'js/ajax_login.js'
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b879d098/initial] (1) [perdir /srv/www/mysite.com/login/] pass through 

/srv/www/mysite.com/login/js/ajax_login.js
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8796080/initial] (3) [perdir /srv/www/mysite.com/login/] strip per-dir 

prefix: /srv/www/mysite.com/login/images/ajax_busy.gif -> images/ajax_busy.gif
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8796080/initial] (3) [perdir /srv/www/mysite.com/login/] applying pattern 

'^([a-z0-9]+/?){0,3}$' to uri 'images/ajax_busy.gif'
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8796080/initial] (1) [perdir /srv/www/mysite.com/login/] pass through 

/srv/www/mysite.com/login/images/ajax_busy.gif
12.345.678.90 - [mysite.com/sid#b85a5dc8][rid#b8796080/initial] (1) [perdir /srv/www/mysite.com/about/] pass through 

/srv/www/mysite.com/about/images/edit.png

As you can see, the about/index.php page contains a css file that is located in /requires. Above, the ip address rule applies to the css file and it gets redirected. The ip address rule gets skipped if I place a .htaccess file in /requires with just the line:

RewriteEngine on
1
  • I found the solution: The .htaccess files in sub directories like /about should have the line: RewriteOptions inherit This makes them inherit the rules from the parent directories .htaccess more info here: rankinglabs.com/Mod_Rewrite#Thank-You-For-Visiting! However, I think I am going make a script for my server that I run to close the site. It will just de-activate subfolder .htaccess files when I want to close the website. Otherwise, the other rules in my root folder will be applied on every page load, and maybe hurt performance. Thanks!
    – moolagain
    May 29, 2010 at 0:56
0

Do this in all the subfolder .htaccess files.

RewriteEngine On
RewriteOptions Inherit

See more: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#RewriteOptions

0

why not doing the ip check with php?

can look sth. like this

< ?php if($_SERVER['REMOTE_ADRR'] != '66.777.888.99') exit; ? >

You must log in to answer this question.