Forum Moderators: phranque

Message Too Old, No Replies

.htaccess help

         

clownenround

12:14 pm on Mar 31, 2011 (gmt 0)

10+ Year Member



The following code is used in .htaccess to only let certain IPs to the site for when it's under maintenance. I'm getting some kind of a redirecting loop error though when my IP isn't added. Can't seem to figure out what the problem is.

The bottom part is a rewrite rule for ExpressionEngine.

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^98\.165\.141\.199$
RewriteRule $ /splash.php [R=302,L]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

jdMorgan

11:31 pm on Apr 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In .htaccess, you must explicitly prevent recursion:

RewriteEngine On
#
# Externally 302-redirect unauthorized-IP-address requests to the splash
# page during maintenance, unless the request is for the splash page itself
RewriteCond %{REMOTE_ADDR} !^98\.165\.141\.199$
RewriteRule !^splash\.php$ /splash.php [R=302,L]
#
# Internally rewrite requests for URLs which do not resolve to images, to physically-existing
# files, or to physically-existing directories to the index.php script, unless already done
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond $1 !^index\.php#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Note that over the years, your server will be much more reliable if you comment the code precisely, and leave the comments in place.

As coded, your splash page may not use any HTTP includes (objuects such as images, css stylesheets, or JavaScript files, since all such requests will also get rewritten to splash.php. If such objects are required to properly render the page, then their URLs must be excluded from the first rule as is presently done in the second rule.

Also, if you can code a small script to do it, it would be better to return a 503-Not Available response along with an appropriate Retry-After HTTP header to all known search engines during maintenance, instead of the 302-Found response used for human visitors.

Jim