Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mod_rewrite : redirecting all pages to another set

0 views
Skip to first unread message

LC's No-Spam Newsreading account

unread,
Oct 6, 2008, 11:58:24 AM10/6/08
to
Our institute has a set of apache servers. What we want to do is to put
up a banner with an important announcement, so that reference to any
page http://hostname/anypath will cause the following :

- if anybody accesses our site for the first time in his session
he will see :

- a popup window with the message (and a close button)
- a short notice saying that he will be redirected to the wished
page in 5 sec (in the main browser window)
- the wished page will soon appear in the main browser window

- for any further access he will be shown the requested page with
no popup

I have already the javascript code which does the above, using a cookie,
so I can do what I want FOR A SPECIFIC PAGE.

In order to do that for a generic page (without inserting the javascript
code everywhere) I have a CGI which can be called as follows :

http://hostname/index.cgi?urlofthepagetobeshown

(the CGI receives the url of the page to be shown in the QUERY_STRING
[and if is null generates an appropriate default to the home page], uses
awk to replace a placeholder in a template file containing the above
javascript code with the actual QUERY_STRING and outputs the generated
HTML+js)

What I need now is a way to replace ANY url of the form

http://hostname/urlofthepagetobeshown

with

http://hostname/index.cgi?urlofthepagetobeshown

mod_rewrite is the obvious candidate, but I cannot find a working
example, and my attempts cause a loop.

At the moment I'm testing all that in a personal directory (later it
should be moved to DocumentRoot). So I have there an .htaccess which
contains

RewriteEngine on
Options FollowSymLinks
RewriteRule ^(.*) /~lucio/temp/temp/index.cgi?$1 [L,NS]

(I have also enabled RewriteLog in the main conf file as root to see
what happens. We already have rewrite_mod in operation to allow our
institute web sites to be accessed with two different domains).

The point is that any url is correctly translated into index.cgi?url
but then the rewritten url is again submitted to translation.

I tried to block the loop putting BEFORE the RewriteRule a
RewriteCond %{REQUEST_URI} !^/\~lucio/temp/temp/index\.cgi

but that causes an HTTP_FORBIDDEN.

How can I force mod_rewrite to rewrite ANY URL as index.cgi?URL with the
exception of index.cgi itself ?


--
----------------------------------------------------------------------
nos...@mi.iasf.cnr.it is a newsreading account used by more persons to
avoid unwanted spam. Any mail returning to this address will be rejected.
Users can disclose their e-mail address in the article if they wish so.

Grant

unread,
Oct 6, 2008, 3:56:05 PM10/6/08
to

You don't, just map index.cgi -> index.cgi?home.html or something, or
use the fake directory cgi option -- lot's of other ways to do this,
perhaps even RTFM? Lots of examples in there.

Grant.
--
http://bugsplatter.id.au/

LC's No-Spam Newsreading account

unread,
Oct 7, 2008, 4:11:01 AM10/7/08
to
On Tue, 7 Oct 2008, Grant wrote:

> On Mon, 6 Oct 2008 17:58:24 +0200, LC wrote:

>> Our institute has a set of apache servers. What we want to do is to put
>> up a banner with an important announcement,

>> RewriteEngine on


>> Options FollowSymLinks
>> RewriteRule ^(.*) /~lucio/temp/temp/index.cgi?$1 [L,NS]

>> The point is that any url is correctly translated into index.cgi?url


>> but then the rewritten url is again submitted to translation.
>>
>> I tried to block the loop putting BEFORE the RewriteRule a
>> RewriteCond %{REQUEST_URI} !^/\~lucio/temp/temp/index\.cgi
>>
>> but that causes an HTTP_FORBIDDEN.
>>
>> How can I force mod_rewrite to rewrite ANY URL as index.cgi?URL with the
>> exception of index.cgi itself ?
>
> You don't, just map index.cgi -> index.cgi?home.html or something, or
> use the fake directory cgi option -- lot's of other ways to do this,
> perhaps even RTFM? Lots of examples in there.

I carefully RTFM, but can't find an example like my case.

What is the " fake directory cgi option" ?
(in general I avoid using /cgi-bin or personal /user-bin and tend to use
local CGIs in the same directory as the rest of the pages)

What do you mean by " map index.cgi -> index.cgi?home.html" ? If
index.cgi is called without arguments, it already provides a default
argument (inside the script).

But this has nothing to do with my issue :

mod_rewrite correctly rewrites ANY_URL as index.cgi?ANY_URL (*)

but then it attempts to rewrite index.cgi itself ! I need a way to stop
this loop

(*) as it should be. The effect of the cgi is to display on one hand a
popup with a banner message, on the other to display the requested
ANY_URL page in the main window

inv...@example.org

unread,
Oct 7, 2008, 6:25:44 AM10/7/08
to


"LC's No-Spam Newsreading account" <nos...@mi.iasf.cnr.it> wrote:

>I carefully RTFM, but can't find an example like my case.

There are four options available to you.

1)
Try various examples you find on the web or in newsgroups
and hope you stumble upon one that does what you want.
Best if what you want is exactly the same as what a bunch
of other people want.

2)
Spend a bunch of time learning mod_rewrite from the ground
up, doing small experiments and doing extensive testing.
Best if you set up websites for a living.

3)
Hire someone with experience to do it for you. If you don't
have any money, get a part time job at McDonald's and use
that money to hire an expert. That will take up less of
your time than doing it yourself will. Best if your time
is worth more than what they pay prisoners.

4)
Give up. Decide that you can live without doing what you
want to do.

LC's No-Spam Newsreading account

unread,
Oct 7, 2008, 9:43:45 AM10/7/08
to
On Tue, 7 Oct 2008, inv...@example.org wrote:

> There are four options available to you.
> 1) Try various examples you find on the web or in newsgroups

> 2) Spend a bunch of time learning mod_rewrite from the ground

> 3) Hire someone with experience to do it for you. If you don't

> 4) Give up. Decide that you can live without doing what you

Thanks for the very unhelpful suggestions :-(

After all I found a way to do what I wanted.

The infinite loop in going from url to index.cgi?url was NOT due to the
fact index.cgi is itself an url subject to rewriting ...

... but to the fact index.cgi does a document.location=url redirection
itself, and therefore causes another rewrite on the target url, and
another, and another.

The way to cut that out was the following :

- I noticed that if I call a page url directly, it has an empty referer
while if I call the page through the CGI, it has a referer equal to
the same page url

- therefore this is what I put in the .htaccess

RewriteCond %{HTTP_REFERER}<->http://%{SERVER_NAME}%{REQUEST_URI} !^(.*)<->\1$
RewriteRule ^(.*).html /~lucio/temp/temp/index.cgi?$1.html [L]

so the rewrite is triggered only if the referer is NOT THE SAME as the
invoked URL.

The condition looks clumsy, because I haven't found a way to test
A==B in a RewriteCond (where both A and B are variables), but to test
that "A concatenated a separator concatenated B" is equal to
"anything concatenated a separator concatenated the same thing" ...

... but I employed the same sort of expression already

(anyhow if anybody has a simpler way of doing such test it will be
definitely welcome)

0 new messages