Forum Moderators: phranque
1 most important: I would like to prevent access to my site by this method [123.123.123.123...]
and ideally just redirect it another site (I only want my site to be accessable via it's domain name)
and would like to be able to write a rule that does this, I found a similar example in the Apache docs and I came up with this:
RewriteRule ^/~johnny* [someothersite.com...] [R,L]
but it just doesn't work at all (Yes the host has mod_rewrite and follow symlinks on, other unrelated rules work ok)
2. I would also love to be able to deny or redirect certain requests based on a string in a URL (ie anywhere in the requested URL). Now sure I've searched through this forum and others but I can't quite the regular expression I'm looking or. And this didn't work as I recall:
RewriteRule .*unwantedrequest.* [someothersite.com...] [R,L]
So thanks a lot for any help
RewriteRule ^~johnny/ http://www.yourcorrectdomain.com/ [R=301,L]
The character "*" means "zero or more of the preceding character" and so is not what you wanted. You probably wanted ".*" whichs means "any character" "zero or more of the preceding character" or, by construction, "zero or more of any character". However, that construct is never needed at the beginning or end of an unanchored pattern, because it is redundant. For more help on anchoring and operators in regular expressions, see the tutorial cited in our forum charter (link above left).
With that in mind, your second rule would be:
RewriteRule unwantedrequest http://www.someothersite.com/ [R=301,L]
However, rather than sending unwanted requests off to some other site (where they may also be unwanted), the simpler and more responsible thing to do is
RewriteRule unwantedrequest - [F]
Jim
I have not been be able to get the first to work though either on my own machine with Apache installed or on my web hosting account and I wonder now if there is a good reason for this.
RewriteRule ^~johnny/ [yourcorrectdomain.com...] [R=301,L]
Where this does work is if I do:
[123.123.123.123...]
I take it it simply isn't 'seeing' the first part of the URL and only subsequent files or directories... I wonder if there is another way of getting .htaccess to 'see' the entire URL using something else. I know with HTTP_REFERER you can but of course referer is such an unreliable variable and easily spoofed. Perhaps HTTP_HOST would be appropriate?
Thanks for the link to the regex tutorial and thanks again for your time. And.. yes you are quite right it is more responsible not to dump unwanted traffic on other sites.
J
However, one variable that does not change is %{THE_REQUEST}, which is the entire original HTTP request in the form:
GET /~johnny/whatever.html HTTP/1.1
So, you may be able to use
RewriteCond %{HTTP_HOST} ^123\.123\.123\.123$
RewriteCond %{THE_REQUEST} \ /~johnny/
RewriteRule (.*) http://www.yourcorrectdomain.com/$1 [R=301,L]
(All bets are off if you're using a "domain forwarding" service to get to a "free" server, though, since this may use a 301 or 302 redirect that defeats the whole scheme.)
Jim
I.e doing:
[123.123.123.123...]
works perfectly, but doing:
[123.123.123.123...]
just asks for a user name and password as usual, which I think is quite strange, considering that the rule you gave (which acts on the entire site) would be processed way before what is happening in another directory.
J
Seems that basic auth on that directory is overriding any rules set in the parent directory.
Perhaps there is something specific I can do on this directory instead to stop it being accessed unless from via the proper domain name.
J
Just copy the rules you need into .htaccess in that directory, modify them for that locale, and see if that helps.
Note that the order of different modules' directives in .htaccess doesn't control invokation order. Each module is invoked and scans .htaccess for the directives it understands. So, execution order is controlled by the order that the modules scan the file, not the order that individual directives appear in the file...
Only the order of directives belonging to the same module is meaningful.
Jim
And I think i got it sorted ok Jim. I had to use a different method though on that directory and searching through this forum gave me some clues. I couldn't redirect it but at least I could prevent access from [123.123.123.123...]
on that directory I put:
SetEnvIf Request_URI "~johnny" ban
as the first thing, then a
deny from env=ban
in between the <Limit GET POST> </Limit> tags (which already had a require valid user of course)
and that works great.
Thanks for all your help Jim, really appreciated. I was pulling my hair out with the mod rewrite stuff..
J