Forum Moderators: phranque

Message Too Old, No Replies

Very basic rewrite help required

.htaccess rewriting keywords

         

Johnathon222

2:13 am on Jan 14, 2005 (gmt 0)

10+ Year Member



Hi there, I want to do 2 things:

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

Johnathon222

2:15 am on Jan 14, 2005 (gmt 0)

10+ Year Member




Sorry my typo that should read:

"I can't quite find the regular expression I'm looking for"

jdMorgan

3:09 am on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In a per-directory .htaccess context, the leading slash is stripped from the local URL-path "seen" by RewriteRule. So that's one reason why your rule didn't work. Use:

RewriteRule ^~johnny/ http://www.yourcorrectdomain.com/ [R=301,L]

I suggest you use a 301-Moved Permanently redirect as shown, so as to tell search engines to use the new URL to list your site.

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]

although your original should have worked anyway. Something else may be wrong with your configuration settings.

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]

which simply returns a 403-Forbidden response and no content.

Jim

Johnathon222

12:46 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Big thanks for the help Jim. The second rule works perfectly.

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

jdMorgan

3:36 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the "/~johnny" is probably being stripped off at the httpd.conf level. Remember that .htaccess works in a per-directory context, so {REQUEST_URI} and the local URL-path seen by RewriteRule change as you progress down the directory structure, and are always "local" or "relative" to the directory in which mod_rewrite examines them.

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]

or some variation on that theme to do what you want.

(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

Johnathon222

7:14 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Jim this is fantastic and many thanks. It works great on everything apart from where I have a directory on the site which is protected by another .htaccess file using Basic Auth; for some strange reason this rule is totally ignored for this particular directory and I'm not too sure why at the moment.

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

jdMorgan

7:17 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See mod_rewrite docs, RewriteOptions inherit.

Jim

Johnathon222

8:06 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



thanks for the hint Jim. I read up on this and tried putting RewriteOptions inherit in the .htaccess file with basic auth and still exactly the same unfortunately. Both before and after the auth statements.

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

jdMorgan

8:40 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess mod_auth is takig priority over mod_rewrite.

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

Johnathon222

11:23 am on Jan 15, 2005 (gmt 0)

10+ Year Member



cheers for the info, I didn't know that about the order of directives.

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