RewriteRule problem

RewriteRule problem

am 24.11.2005 14:52:40 von Colin Messitt

I'm sure this should be simple, but it's beaten me.

I have a URL - http://www.example.com/aaaaa

I want to direct this to a PHP script, and pass the originally requested URL
to it.

I create a .htaccess file containing ...

RewriteEngine On
RewriteRule ^(.*) redirect.php?p=$1

which I think should convert that URL to

http://www.example.com/redirect.php?p=http://www.example.com /aaaaa

but it doesn't seem to. My simple PHP script is as follows...

echo $_SERVER['QUERY_STRING'];
?>

my script seems to be called, but it outputs ...

p=redirect.php

i.e. the translated URL is being passed as the parameter, and not the
original URL which is what I want.

I'm obviously doing something silly, but what?

Colin.

Re: RewriteRule problem

am 24.11.2005 19:30:55 von Purl Gurl

Colin Messitt wrote:

> I have a URL - http://www.example.com/aaaaa

> I want to direct this to a PHP script, and pass the originally requested URL
> to it.

> I create a .htaccess file containing ...

> RewriteEngine On
> RewriteRule ^(.*) redirect.php?p=$1

> which I think should convert that URL to

> http://www.example.com/redirect.php?p=http://www.example.com /aaaaa

> but it doesn't seem to. My simple PHP script is as follows...

> > echo $_SERVER['QUERY_STRING'];
> ?>


Place this .htaccess file in your /aaaaa directory,

RewriteEngine on
RewriteRule .* /redirect.php?p=http://www.example.com%{REQUEST_URI}

Carefully note there is a space between .* and /redirect.php in case your
reader font does not show that space clearly.

That will redirect to your document root / redirect.php as if your URL is,

http://www.example.com/redirect.php

You can hardcode in /aaaaa in place of request uri if you like. However using
the request uri will make your .htaccess generic in nature; use anywhere
without having to hardcode the actual (final) directory location. Some adjustments
to the hardcode path might be needed for different circumstances.

Your php script will then display the query string you are expecting.

Purl Gurl

Re: RewriteRule problem

am 24.11.2005 19:52:21 von Colin Messitt

"Purl Gurl" wrote in message
news:yrGdnUIE7sBnmxveRVn-iA@giganews.com...
> Colin Messitt wrote:
>
>> I have a URL - http://www.example.com/aaaaa
>
>> I want to direct this to a PHP script, and pass the originally requested
>> URL
>> to it.
>
>> I create a .htaccess file containing ...
>
>> RewriteEngine On
>> RewriteRule ^(.*) redirect.php?p=$1
>
>> which I think should convert that URL to
>
>> http://www.example.com/redirect.php?p=http://www.example.com /aaaaa
>
>> but it doesn't seem to. My simple PHP script is as follows...
>
>> >> echo $_SERVER['QUERY_STRING'];
>> ?>
>
>
> Place this .htaccess file in your /aaaaa directory,
>
> RewriteEngine on
> RewriteRule .* /redirect.php?p=http://www.example.com%{REQUEST_URI}
>
> Carefully note there is a space between .* and /redirect.php in
> case your
> reader font does not show that space clearly.
>
> That will redirect to your document root / redirect.php as if your URL
> is,
>
> http://www.example.com/redirect.php
>
> You can hardcode in /aaaaa in place of request uri if you like. However
> using
> the request uri will make your .htaccess generic in nature; use anywhere
> without having to hardcode the actual (final) directory location. Some
> adjustments
> to the hardcode path might be needed for different circumstances.
>
> Your php script will then display the query string you are expecting.
>
> Purl Gurl

Seems to suffer the same problem, if I navigate to
http://www.example.com/zzzzz the query string comes back as

p=http://www.example.com/redirect.php

instead of containing the original URL requested.

Colin.

Re: RewriteRule problem

am 24.11.2005 20:44:12 von Purl Gurl

Colin Messitt wrote:

> > Colin Messitt wrote:
> Purl Gurl wrote:

> > Place this .htaccess file in your /aaaaa directory,

> > RewriteEngine on
> > RewriteRule .* /redirect.php?p=http://www.example.com%{REQUEST_URI}

> > Your php script will then display the query string you are expecting.

> Seems to suffer the same problem, if I navigate to

> http://www.example.com/zzzzz the query string comes back as

You have missed the point,

"Place this .htaccess file in your /aaaaa directory...."

Do the same for your zzzzz directory or write a parent directory
..htaccess to handle this for all directories. Lot easier and more
efficient to do this on a per-directory basis.

You have to hardcode in a "www...something" URL for this. You
can use other environment variables but none will provide a
"www" translated URL path.

You must place that type of .htaccess in each directory,
short of writing a global .htaccess in the parent directory
which is not suggested for efficiency reasons.

Purl Gurl

Re: RewriteRule problem

am 25.11.2005 10:42:57 von Colin Messitt

"Purl Gurl" wrote in message
news:d7KdnQ2wuLu0hRvenZ2dnUVZ_v6dnZ2d@giganews.com...
> Colin Messitt wrote:
>
>> > Colin Messitt wrote:
>> Purl Gurl wrote:
>
>> > Place this .htaccess file in your /aaaaa directory,
>
>> > RewriteEngine on
>> > RewriteRule .* /redirect.php?p=http://www.example.com%{REQUEST_URI}
>
>> > Your php script will then display the query string you are expecting.
>
>> Seems to suffer the same problem, if I navigate to
>
>> http://www.example.com/zzzzz the query string comes back as
>
> You have missed the point,
>
> "Place this .htaccess file in your /aaaaa directory...."
>
> Do the same for your zzzzz directory or write a parent directory
> .htaccess to handle this for all directories. Lot easier and more
> efficient to do this on a per-directory basis.
>
> You have to hardcode in a "www...something" URL for this. You
> can use other environment variables but none will provide a
> "www" translated URL path.
>
> You must place that type of .htaccess in each directory,
> short of writing a global .htaccess in the parent directory
> which is not suggested for efficiency reasons.
>
> Purl Gurl

Ah no, I see why we are at cross purposes now. There isn't a /aaaaa
directory, and there isn't supposed to be. I'm trying to get an effect like
TinyUrl, where the user can use links such as

http://www.example.com/111
http://www.example.com/222
http://www.example.com/333

and the same PHP script is called each time, and the name of the URL the
user originally requested is passed to the script, so the script can detect
that the user used 111 or 222 or 333.

Colin.

Re: RewriteRule problem

am 25.11.2005 16:48:57 von Purl Gurl

Colin Messitt wrote:

(snipped)

> Ah no, I see why we are at cross purposes now. There isn't a /aaaaa
> directory, and there isn't supposed to be.

In the future, state your true parameters. You have misled readers
which wastes time of readers. Chances are, some will no longer
respond to your articles because of your wasting reader's time.

> I'm trying to get an effect like
> TinyUrl, where the user can use links such as

> http://www.example.com/111
> http://www.example.com/222
> http://www.example.com/333

> and the same PHP script is called each time, and the name of the URL the
> user originally requested is passed to the script, so the script can detect
> that the user used 111 or 222 or 333.

This is not a good way to do this. Apache is not designed to be redirection server.
Processing redirections through htaccess files is CPU intensive. Use of htaccess
files should be kept to absolute bare minimum. This is even more true when a
global htaccess is used in a primary parent directory, such as document root.

For every single request, regardless if a request matches your rewrite code,
the htaccess must be processed before any URL can be served. So, you have
users requesting normal pages, the htaccess is processed, then a page is served,
and users request your special pages which also requires htaccess processing.

Point is, normal requests invoke the htaccess which is not needed; every
single request invokes the htaccess for processing.

Use a direct URL instead,

http...yourhost/visitors/login/redirect.php?1234

Even so, you are still invoking more processes than needed. You are invoking
a script to redirect to another URL somewhere on your server. Adding to this
you create more problems with a redirect script.

For Mozilla (Netscape) browsers, in a script, you can use,

print "Location: http...yourhost/some/page.html";

Works fine, except for MSIE browsers and a few others, for specific circumstances.

For MSIE, for selected tasks, you have to use a meta refresh requiring print of an html document.

This is exceptionally true for a case example of,

print "Location: http...yourhost/redirect.php?file=rock.mid";

MSIE cannot follow a redirection URL with a query string of that nature.
There are many other case example under which MSIE fails; MSIE
is a severely broken browser.

Bottom line is when you turn Apache into a redirection server, you will
have a very slow server and nothing but problems.

Your original article indicates you want a full URL in your redirection,

http...yourhost/redirect.php?http...yourhost/aaaaa

You do not need a full URL in your query string,

RewriteCond %{REQUEST_URI} ^/[0-9]+$
RewriteRule (.*) /redirect.pl?p=$1 [R=301,L]

That will create a query string containing your numbers. Your
php script can pull those numbers with QUERY_STRING,
then create a full URL, as needed.

$url = "http...yourhost/$ENV{QUERY_STRING}"; (Perl method)

Use of a "permanently moved 301" status will alert smart browsers
and spider bots, such as google and yahoo, to request the URL
directly, in the future.

All of this will make a mess of your Apache server. I urge you to not
turn your Apache server into a "tinyurl" server. Doing so is illogical,
highly inefficient and will create lots of bugs.

Instead, create a directory per user,

http...yourhost/users/1234

Drop an index.html or index.cgi or your favorite method into
each directory, and you are set to go, with speed and efficieny
and a minimum amount of bugs, if not no bugs.

Work towards writing articles which are clear, concise and coherent.

Purl Gurl

Re: RewriteRule problem

am 25.11.2005 17:32:26 von Colin Messitt

And the fact that this is going to be on a stand-alone in-house server that
is going to get hit maybe 100 times a day and will be doing nothing else
makes your points about efficiency etc. relevant how exactly? Or the fact
that IE is broken in loads of unrelated areas is relevant how exactly to
this? Or your seeming desire to waste time and money doing special tricks
for different browsers is relevant how exactly? And how is your comment
about "directory per user" relevant? There is only one user, you are making
assumptions about the scenario and the context of the query with absolutlely
no substance on which to base them.

You made an assumption that was incorrect about the parameters of my query.
Admittedly there wasn't enough information to define the problem precisely
and that was a failure in my framing of the query, but your assumption was
wrong. Thing is, I'm not perfect, I don't pretend to be. I try to explain
myself perfectly and without any possible ambiguity, but I don't always
manage it. Sometimes a follow-up for clarification is needed, as is the case
with lots of people that try to explain themselves to me. But I'm sensible
and understanding enough to realise that people aren't perfect and try to
accomodate their unintentional slips, a social and human skill that seems to
have bypassed you.

Colin.

"Purl Gurl" wrote in message
news:eYCdnWwqUJ8XrxreRVn-ow@giganews.com...
> Colin Messitt wrote:
>
> (snipped)
>
>> Ah no, I see why we are at cross purposes now. There isn't a /aaaaa
>> directory, and there isn't supposed to be.
>
> In the future, state your true parameters. You have misled readers
> which wastes time of readers. Chances are, some will no longer
> respond to your articles because of your wasting reader's time.
>
>> I'm trying to get an effect like
>> TinyUrl, where the user can use links such as
>
>> http://www.example.com/111
>> http://www.example.com/222
>> http://www.example.com/333
>
>> and the same PHP script is called each time, and the name of the URL the
>> user originally requested is passed to the script, so the script can
>> detect
>> that the user used 111 or 222 or 333.
>
> This is not a good way to do this. Apache is not designed to be
> redirection server.
> Processing redirections through htaccess files is CPU intensive. Use of
> htaccess
> files should be kept to absolute bare minimum. This is even more true when
> a
> global htaccess is used in a primary parent directory, such as document
> root.
>
> For every single request, regardless if a request matches your rewrite
> code,
> the htaccess must be processed before any URL can be served. So, you have
> users requesting normal pages, the htaccess is processed, then a page is
> served,
> and users request your special pages which also requires htaccess
> processing.
>
> Point is, normal requests invoke the htaccess which is not needed; every
> single request invokes the htaccess for processing.
>
> Use a direct URL instead,
>
> http...yourhost/visitors/login/redirect.php?1234
>
> Even so, you are still invoking more processes than needed. You are
> invoking
> a script to redirect to another URL somewhere on your server. Adding to
> this
> you create more problems with a redirect script.
>
> For Mozilla (Netscape) browsers, in a script, you can use,
>
> print "Location: http...yourhost/some/page.html";
>
> Works fine, except for MSIE browsers and a few others, for specific
> circumstances.
>
> For MSIE, for selected tasks, you have to use a meta refresh requiring
> print of an html document.
>
> This is exceptionally true for a case example of,
>
> print "Location: http...yourhost/redirect.php?file=rock.mid";
>
> MSIE cannot follow a redirection URL with a query string of that nature.
> There are many other case example under which MSIE fails; MSIE
> is a severely broken browser.
>
> Bottom line is when you turn Apache into a redirection server, you will
> have a very slow server and nothing but problems.
>
> Your original article indicates you want a full URL in your redirection,
>
> http...yourhost/redirect.php?http...yourhost/aaaaa
>
> You do not need a full URL in your query string,
>
> RewriteCond %{REQUEST_URI} ^/[0-9]+$
> RewriteRule (.*) /redirect.pl?p=$1 [R=301,L]
>
> That will create a query string containing your numbers. Your
> php script can pull those numbers with QUERY_STRING,
> then create a full URL, as needed.
>
> $url = "http...yourhost/$ENV{QUERY_STRING}"; (Perl method)
>
> Use of a "permanently moved 301" status will alert smart browsers
> and spider bots, such as google and yahoo, to request the URL
> directly, in the future.
>
> All of this will make a mess of your Apache server. I urge you to not
> turn your Apache server into a "tinyurl" server. Doing so is illogical,
> highly inefficient and will create lots of bugs.
>
> Instead, create a directory per user,
>
> http...yourhost/users/1234
>
> Drop an index.html or index.cgi or your favorite method into
> each directory, and you are set to go, with speed and efficieny
> and a minimum amount of bugs, if not no bugs.
>
> Work towards writing articles which are clear, concise and coherent.
>
> Purl Gurl

Re: RewriteRule problem

am 25.11.2005 19:24:42 von Purl Gurl

Colin Messitt wrote:

(snipped)

It is precisely the attitude you display in your
immediately previous article which will prompt
readers to ignore you.

Your "top posting" alone is enough to have
readers killfile you. That, however, I overlook
because so many have yet to learn how to
correctly format a response posting. I am
forgiving on that.

You are not to be offended by a person who
speaks in a candid nature. Your first article
is misleading, is poorly written in retrospect,
both of which are annoying. That is nothing
more than candid truth.

A writer is solely responsible for content of
his written material. A writer is also solely
responsible for accepting consequences of
that which he writes.

Years back, two of us teachers are to give
a presentation before hundreds of people,
which I still do on a regular basis.

We glance each other over for an appearance
check before walking out under the spotlight.
My friend has a rather noticeable wet booger
hanging from her nose. I tell her about the
booger, and she is appreciative, but somewhat
embarrassed as expected.

I was presented with a choice. Tell her a booger
is hanging from her nose, or not tell her and have
her discover that booger, later, with rather dire
embarrassment or even mortification. After dying,
she would think, "Why didn't Kira tell me about
that big wet ugly booger hanging from my nose?
That bitch!"

Next we encounter each other, I won't tell you about
that booger hanging from your nose; I will allow readers
to see you for the snotty nosed brat you are.

Purl Gurl