Forum Moderators: phranque

Message Too Old, No Replies

.htaccess HTTP HOST RewriteRule

         

cyberdyne

8:04 pm on Jan 27, 2012 (gmt 0)

10+ Year Member



I'm trying to send fairly regular (attempted) spammers away from my forum to Google.

They arrive with a hit on a particular file but I cannot get RewriteRule to work for a specific url, although it works elsewhere in my htaccess for directories.

I currently have the below:
RewriteCond %{HTTP_HOST} ^domain\.fu$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.fu$ [NC]
RewriteRule ^/forum/index\.php?action=profile;u=88$1 http:\\www.google.com [R=301,L]


Thanks in advance for any assistance.

g1smd

8:21 pm on Jan 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule RegEx pettern matching looks only at the PATH part of the request.

If you want to match query string data you need a preceding RewriteCond looking at either QUERY_STRING or THE_REQUEST.

Your two existing RewriteCond lines can be combined if you use (www\.)? making that part optional. However, do you really need to test that they are accessing your domain name? The requests arrived at your server, so they must have requested your domain name. HTTP_HOST matches YOUR server hostname.

The default action in a redirect is to reappend the original query string parameters. You should take steps to remove them.

cyberdyne

10:03 pm on Jan 27, 2012 (gmt 0)

10+ Year Member



No, I don't need the domain test, you're correct, thank you for your help.

So far, I've come up with the below but still no luck as yet. I'll keep adjusting it:

RewriteCond %{THE_REQUEST} /forum/index\.php\?action\=profile\;u\=88\ HTTP/
RewriteRule ^http\:\/\/www\.google\.com [R=301,L]

g1smd

10:09 pm on Jan 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remove the escaping on colons, slashes, commas and equals signs, as it isn't needed.

You are missing a RegEx pattern in the Rule. This pattern will match only the requested PATH.

Remove all escaping from the target URL in the rule.

Begin the RegEx pattern for the Condition with
^[A-Z]{3,9}\ /

lucy24

10:16 pm on Jan 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... setting aside the question of whether redirecting unwanted visitors to some other site-- even g###-- is really an appropriate, desirable or even legal solution ;)

If a simple [F] isn't emotionally satisfying enough, redirecting to 127.0.0.1 is a perennial favorite. Redirecting back to the source's own IP is another one.

cyberdyne

10:25 pm on Jan 27, 2012 (gmt 0)

10+ Year Member



I now have the following, which I'm guessing should work but still does not I'm afraid.

With regards to the ethics of what I'm trying to do, I agree. Redirecting there was just an idea, 127... sounds even better.

Thank you again for your help ...and tuition ;-)


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /forum/index\.php\?action=profile;u=88\ HTTP/
RewriteRule ^http://www\.google\.com [R=301,L]

g1smd

10:31 pm on Jan 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The rule needs

RewriteRule ^pattern target [flags]

You have no pattern.

The pattern will match the path part of the requested URL, and only the path part.

cyberdyne

10:54 pm on Jan 27, 2012 (gmt 0)

10+ Year Member



Phew! The following works (thank you), although the resulting 'domain.fu' url ends in '?action=profile%3bu=88' which (for some reason) I'm feeling guilty about! (should I be?!)


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /forum/index\.php\?action=profile;u=88\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www\.domain\.fu/ [R=301,L]


Thank you

g1smd

11:16 pm on Jan 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



None of the target URL needs escaping.

To remove the query string add a question mark to the end of the target URL. This tells mod_rewrite to leave the query string off when it sends the redirect header.

cyberdyne

11:36 pm on Jan 27, 2012 (gmt 0)

10+ Year Member



Sorry, copy and paste error, the actual code didn't have escaping on the target url (I did note that earlier).

The question mark worked perfectly, for future reference the complete code is below.

Many thanks for your help.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /forum/index\.php\?action=profile;u=88\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php http://www.domain.fu? [R=301,L]

g1smd

11:55 pm on Jan 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the Rule you allow any depth of folders with
(([^/]+/)*)


In the condition you allow only /forum/ as a valid folder.

Both pieces of code should be the same, either one folder or all...

cyberdyne

12:42 am on Jan 28, 2012 (gmt 0)

10+ Year Member



Understood, thank you again.