Forum Moderators: phranque

Message Too Old, No Replies

.htaccess mod_rewrite if then scenario

         

kburch

11:52 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



I have the following rewrite working ...

RewriteCond %{HTTP_REFERER}!^http://domain.com [NC]
RewriteCond %{HTTP_REFERER}!^http://www.domain\.com/* [NC]
RewriteCond %{HTTP_REFERER}!^http://.+\.domain\.com/* [NC]
RewriteRule /* [domain.com...] [R,L]

My problem is ... that I have one registration URL like this:

[domain.com...]

Since this script is located inside a subdirectory of the protected area, I keep getting bounced out ...

I'm sure there has to be a way to make a similar .htaccess file in the subdirectories that will ignore the previous one ... OR write some time of if/then scenario where "THIS URL" will not rewrite, but anything else matching the rewrite conditions will ...

Anyone got any suggestions?

Thanks!

jdMorgan

12:20 am on Sep 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



kburch,

Welcome to WebmasterWorld!

You can exclude that URL easily. You can also exclude some or all of the specific query string if you like. (Note that a query string is attached to a URL, but is not considered to be part of a URL).


RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain\.com [NC]
RewriteCond %{REQUEST_URI}<->%{QUERY_STRING} !^/folder/index\.php<->.*name=Signup
RewriteRule .* http://www.domain.com/reject.html [R,L]

The first RewriteCond above functionally replaces the first three RewriteConds in the code you posted. The second RewriteCond above uses a trick to access two variables on the same line -- the "<->" characters are completely arbitrary, and serve only to demarcate the expected boundary between the two strings.

I note a pattern of mis-using "*" in your code. In the regular expressions used by mod_rewrite for pattern matching, "*" is a special character than means "any number of the previous character." Similarly, "." is a special character than means "any single character". If you wish to match a literal period or asterisk, you must precede it with a backslash, e.g. "\." as used above. The usage of special characters in regular expressions is much different than that of MSDOS and other operating systems, and much more powerful.

For more information on this subject, see the regular expressions tutorial cited in our forum charter [webmasterworld.com].

Jim

kburch

1:07 am on Sep 15, 2004 (gmt 0)

10+ Year Member



Thanks for your quick response.

I'm still having the same problem with the redirect throwing me back to the rejection page when attemtping to access the account setup as I mentioned before ...

Here's what I put in the .htaccess per your code ...

AuthType Basic
RewriteEngine On
RewriteCond %{HTTP_REFERER}!^http://.+\.avs1\.com/* [NC]
RewriteCond %{HTTP_REFERER}!^http://.+\.avs2\.com/* [NC]
RewriteCond %{HTTP_REFERER}!^http://(.+\.)?mydomain\.com [NC]
RewriteCond %{REQUEST_URI}<->%{QUERY_STRING}!^/nuke/modules/\.php<->.*name=Your_Account
RewriteRule /* [mydomain.com...] [R,L]

Any clue as to why this isn't working?

Thanks again!

jdMorgan

1:18 am on Sep 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The local URL-path to the script is exactly this?

/nuke/modules/.php

That is what your pattern will match, and that the only file which will be excluded from the redirect (if the query is correct, too).

There are many other things that can cause problems -- Apache modules loaded in the wrong order, and such. But the most likely cause is regular-expressions errors. You have re-introduced several by using "/*" but I'll refer you to my previous post on that.

Jim

kburch

2:13 am on Sep 15, 2004 (gmt 0)

10+ Year Member



You pointed out my error! It was an issue with modules/.php instead of the ACTUAL modules.php

Makes ALL the difference!

The /* that you referenced was supplied by the AVS company ... I have since removed the wildcards and all seems to be well.

Thanks for your help!

jdMorgan

2:54 am on Sep 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, cleaning it all up, you should have arrived at:

RewriteCond %{HTTP_REFERER} !^http://.+\.avs1\.co[b]m[/b] [NC]
RewriteCond %{HTTP_REFERER} !^http://.+\.avs2\.co[b]m[/b] [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mydomain\.com [NC]
RewriteCond %{REQUEST_URI}<->%{QUERY_STRING} !^/nuke/module[b]s\.p[/b]hp<->.*name=Your_Account
RewriteRule [b].*[/b] http://www.mydomain.com/buzz.html [R,L]

The problem with adding "/" to the end of domain names when checking HTTP_REFERER is that there is the possibility that the referer may have a port number appended to it, as in "avs1.com:80" In that case, your rule would fail, even though the referer is valid. And then the regex was wrong, in that "/*" means, "match avs1.com followed by any number of contiguous slashes, which is obviously not what you'd want.

The same goes for the pattern in the rule itself; A pattern of "/*" would match any number of slashes, including zero. It's not so much that it is wrong, but that it is confusing. A pattern of ".*" means, "match any number of any characters" or simply put, "match anything".

Jim