Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite & HTTP HOST

         

wilderness

12:11 pm on Sep 29, 2012 (gmt 0)

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



There's an ongoing thread, which already seems confusing enough for the person seeking help, thus I did not wish to hijack same thread.

Could somebody explain two things?
1) Would the following lines function?
2) What options are available in the first part of the RewriteRule (i. e., .*)

#Rewrite requests to sub-domain to domain, except IP's
RewriteCond %{HTTP_HOST} ^testing.example.co.uk
RewriteCond %{REMOTE_ADDR} !^012\.345\. [OR]
RewriteCond %{REMOTE_ADDR} !^123\.456\.
RewriteRule .* http://www.example.net/

lucy24

11:55 pm on Sep 29, 2012 (gmt 0)

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



Would the following lines function?

For a given definition of "function". Did you intentionally leave off all flags in order to strip the question to its bare minimum, or is the absence of flags part of the question?

When there is no [L] (or L-equivalent such as [F] or [G]) then mod_rewrite will continue looking for other things to do. It's counter-intuitive, but a redirect doesn't carry an automatic [L]. There is a more serious issue but I'll get to that.

2) What options are available in the first part of the RewriteRule (i. e., .*)

As you know from other threads, an all-encompassing .* in the Pattern is a last resort. Use it only when the offender might ask for any file of any kind. Most RewriteRules can be constrained to requests for a particular filetype, like pages or images. You do this by using anchors: ending $ and possibly beginning ^ in combination with some literal text.

Request for front page only:

!. in htaccess (meaning "the empty request"-- a rare case of using ! in the pattern)
^/$ in config file

Request for any other page, if site uses extensions (here I use .html as an example):

(/|\.html)$

Request for either in htaccess but don't quote me on this one:

^([^.]+(/|\.html))?$

Request for page, if site doesn't use extensions:

^([^.]*)$

(will not work on ::cough-cough:: sites like apache dot org, because they will have literal . in the body of their URLs so you have to get more complicated)

Request for image:

\.(jpe?g|png|gif)$

3) Exactly what does the rule, as written, do?

You didn't ask this, but it seemed best to include it for completeness.

Every time a request of any kind is received

.* in RewriteRule

look at Conditions. If the request is for a file whose full name begins

testingXexampleXcoXuk

(with possibility of following port number, but nothing before "testing") where X means "any single character of any kind" then proceed to the second Condition. In practice this will be testing.example.co.uk simply because no other request will reach the server, so in this isolated case the . for \. error is not lethal.

Second Condition: If the request is coming in from a place whose IP does not begin with "012.345" (I assume that was made up, since multi-digit IP segments don't have leading zeros) OR does not begin with "123.456" then execute the rule.

Since anything in the form "not A OR not B" will always apply-- assuming A and B are the same type of information, as they are here-- this Condition will always be met. So the only true Condition is the HTTP_HOST one. ("not-A or not-B" is not the same thing as "not {A or B}" i.e. "not-A AND not-B")

I suspect however that those ! in the Conditions sneaked in behind your back, and what you really meant was "IS either A or B". Or, conversely, the OR is a mistake and you meant "is not A AND ALSO is not B". I should also assume that you forgot to escape the literal periods in the first Condition. Like all computers, Apache is fairly stupid and can't tell the difference between a typo and a real mistake, or between either one and the author's intention.

Quick edit: Oops, almost forgot to say what the Rule does in the end. It takes all requests aimed at testingXexampleXcoXuk and sends them to the front page of www.example.com

[edited by: lucy24 at 12:04 am (utc) on Sep 30, 2012]

g1smd

12:03 am on Sep 30, 2012 (gmt 0)

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



typo alert:

>> look at Conditions. If the request is for a file whose full name begins

.. look at Conditions. If the request is for a hostname whose full name begins

lucy24

12:05 am on Sep 30, 2012 (gmt 0)

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



Heh. We overlapped; I was attacking other typos meanwhile. I was thinking "full name" in the address-bar sense:

testing.example.co.uk/directory/morestuff/et cetera

And that's example dot net at the end, not example dot com.

wilderness

12:09 am on Sep 30, 2012 (gmt 0)

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



Many thanks lucy.

phranque

9:18 am on Sep 30, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month




RewriteRule .* http://www.example.net/

it should be noted that the status code for this redirect is a 302.
in search engine terms this is a temporary redirect and the testing.example.co.uk urls will all be indexed with the www.example.net home page content.

wilderness

3:00 pm on Sep 30, 2012 (gmt 0)

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



FWIW, and to clear up the confusion I caused.

I was looking for an alternative method [webmasterworld.com] of restricting access (mod_rewrite) by allowing only specific IP's with a sub-doomain (the IP's would basically be the major SE's and others whom are temporarily allowed access.)

The rest would be Rewrites back to the about, signup and/or 403 page. The method would also require exceptions for Contact pages and Contact forms.

I've made this changes simply for clarification of my intent

#Rewrite requests to sub-domain to domain, except IP's
RewriteCond %{HTTP_HOST} ^testing.example.net
RewriteCond %{REMOTE_ADDR} !^012\.345\. [OR]
RewriteCond %{REMOTE_ADDR} !^66\.249\.(6[4-9]|[78][0-9]|9[0-5])\.
RewriteRule .* about.html [L]

Many thanks to all.

lucy24

9:24 pm on Sep 30, 2012 (gmt 0)

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



Oops, you've still got "not A or not B". That won't work. From your description it sounds as if you need "not A AND not B"-- which is good, because you can simply leave out the [OR] connector.

The Rule picks up all requests, including non-pages. Is that intentional?


Only yesterday I got my nose rubbed in a situation where a blocked human visitor-- that is, their browser-- can still request image files. It's done via Translate. D'oh!

wilderness

9:47 pm on Sep 30, 2012 (gmt 0)

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



Oops, you've still got "not A or not B". That won't work. From your description it sounds as if you need "not A AND not B"-


I don't grasp it, however I'll try it without the [OR]'s and see what happens.
FWIW, I've only provided two examples for sake of simplicity, and I'll have many more IP lines in the working version.


The Rule picks up all requests, including non-pages. Is that intentional?


Yes.

Thanks again.