Forum Moderators: phranque

Message Too Old, No Replies

Second portion of RewriteRule runs without meeting the required cond

         

kwngian

4:13 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



Hi,

Hope some gurus here can help.

Having problem with the following in my .htaccess file;

RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^222.164. [OR]
RewriteCond %{REMOTE_ADDR} ^222.165.
RewriteCond %{REQUEST_URI}!^/fbn/
RewriteRule (.*) /fbn/ [R=301,L]

RewriteCond %{REMOTE_ADDR} ^201.11. [OR]
RewriteCond %{REMOTE_ADDR} ^202.16. [OR]
RewriteCond %{REMOTE_ADDR} ^215.24.44.
Redirect permanent ^(.*)$ http://www.example.com$1

Second portion of the RewriteCond runs even if it does not meet the conditions stated.

Any advice appreciated.

Thanks.

jdMorgan

4:17 pm on Aug 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because you've tried to mix directives from two modules, mod_alias [httpd.apache.org] and mod_rewrite [httpd.apache.org]. That won't work. You need to use RewriteRule, not Redirect Permanent.

Jim

kwngian

4:33 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



Hi Jim

Thanks for your help. I tried the following but it is still redirecting the second portion even when the conditions are not met.

RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^222.164. [OR]
RewriteCond %{REMOTE_ADDR} ^222.165.
RewriteCond %{REQUEST_URI}!^/fbn/
RewriteRule (.*) /fbn/ [R=301,L]

RewriteCond %{REMOTE_ADDR} ^201.11. [OR]
RewriteCond %{REMOTE_ADDR} ^202.16. [OR]
RewriteCond %{REMOTE_ADDR} ^215.24.44.
RewriteRule (.*) http://www.example.com$1

Any other error you're able to spot from the above?

Thanks.

jdMorgan

5:15 pm on Aug 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, assuming the code is for use in .htaccess, I'd rewrite the code like this:

RewriteEngine On
#
RewriteCond %{REMOTE_ADDR} ^222\.164\. [OR]
RewriteCond %{REMOTE_ADDR} ^222\.165\.
RewriteRule !^fbn/ http://{HTTP_HOST}/fbn/ [R=301,L]
#
RewriteCond %{REMOTE_ADDR} ^201\.11\. [OR]
RewriteCond %{REMOTE_ADDR} ^202\.16\. [OR]
RewriteCond %{REMOTE_ADDR} ^215\.24\.44\.
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

If you are testing access-control code like this, be sure to flush your browser cache after any change to the code or after accessing a page from an "allowed" address and before testing it from a "denied" address.

Otherwise, your browser may show you a page it has cached, and not send the request to the server. If this happens, then the access-control code on your server can have no effect.

It isn't clear exactly what your goal here is, but note that if this code is intended to deflect the blocked-IP's requests to two specific pages on your site, then there is no need to use external redirects -- internal rewrites will be faster, more effective, and will 'hide' the action that your mod_rewrite code is taking from visitors. They will see the original URL, but the 'Your Access Denied' content.

Jim

kwngian

5:41 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



Thanks Jim, will give it a try. And I will be trying it from a non-live site from now on. Kind of stressful having all the visitors getting 404 and other odd errors.