Forum Moderators: phranque

Message Too Old, No Replies

Rewrite mod works but. only 1 time

From www.website.com/name to website.com/name.php?id=xx

         

uncoke

5:49 pm on Mar 12, 2008 (gmt 0)

10+ Year Member



Hello and thanks for this cool forum.

I m a noob with a noob question, i have studied all days and I m near to the solution but i need your help!

I have a website where my users have a public page at the url:

http://www.website.com/name.php?id=xx

where xx is the id in the mysql DB

I would like a redirect to a more simple url:

http://www.website.com/nameofuser

So I have created a REWRITEMAP to a php prg that return the nameofuser starting from the id (I'm cool! I made it myself!). And it is working fine!

in the .htaccess i have wrote:

  RewriteEngine On
RewriteMap team prg:/home/www/web/include/userdir.php
RewriteCond %{HTTP_HOST} ^([^:]+)
RewriteRule ^/([^/\.]+)/?$ /team.php?id=${team:$1} [R]

And it seem to work too! But only first time after the restarting of apache...

so if i retstart the server and then i go to the url:

www.website.com/myteam

I see the url to change in code]www.website.com/team.php?id=12[/code]
Then if i write again code]www.website.com/myteam[/code]
I see the url to change in code]www.website.com/team.php?id=[/code] with any id !?!

I have made a lot of test and now I m really confused! I dont undestand if it is a probelm of the rewrite rule or i have a mistake in the prg php.

Can someone help me ?

I have another (noob) question about the rewriterule, in the exemple above I see the url to change, but if i don't want it i take off the [R], I have a redirect but the URL is the same, thi is good apart...that every element in the page is missing! Missing link, missing img etc... I suppose i have to correct the Rule but at moment I m very confused!

Thank for your help!

dean01

6:39 pm on Mar 12, 2008 (gmt 0)

10+ Year Member



Hi,
I have the same problem I think and it's driving me nuts !

If I boune apache or change and reload the htaccess file it works....for a short while and then I get 404 errors...arggghhh...

I'd be very grateful for ANY help.

my htaccess is :
Options +FollowSymLinks
RewriteBase /store/
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule .* - [L]
RewriteRule ^sitemap.xml sitemap.php [QSA,L]
RewriteRule ^install.php install.php [QSA,L]
RewriteRule ^pict/([^/]+)?$ [thumbs.ebaystatic.com...] [QSA,L]
RewriteRule ^item123/([^/]+)?$ item123.php?i=$1 [QSA,L]
RewriteRule ^item123/view/(.*)?$ item123.php?i=view&u=$1 [QSA,L]
RewriteRule ^register.php item.php?i=register [QSA,L]
RewriteRule ^bans.php item.php?b=bans [QSA,L]
RewriteRule ^search/([^/]+)?$ index.php?mainCat=search&q=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)?$ index.php?mainCat=$1&subCat=$2 [QSA,L]
RewriteRule ^([^/]+)?$ index.php?mainCat=$1 [QSA,L]
~

Cheers

Dean

uncoke

9:13 pm on Mar 12, 2008 (gmt 0)

10+ Year Member



#2 Question Resolved writing the rule without [R]

#1 - I have made another test without the rewriteMap with the code

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^([^:]+)
RewriteRule ^/([^/\.]+)/?$ /team.php?id=3

and this works ,every time I go to the url i see the

team.php?id=3
page

Last test was changing the php prg called by the RewriteMap, i have deleted all the lines and it rerurn a simple "3". But the music is always the same. It works only the first time I go to the url. The second time i m redirected to

team.php?id=
without the "3"!

but why ? What I'm missing ?!?!

dean01

11:31 pm on Mar 12, 2008 (gmt 0)

10+ Year Member



Hi Uncoke,
Thanks for your help. I too am a newbie....
Do you mean that my problem would be resolved by including the [R] flag like this..[R,QSA,L].
If so, for which rules.
If not....Errr hen I'm confuzzed.

Thanks again

jdMorgan

11:32 pm on Mar 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only times I've ever used 'active' RewriteMaps, I've used PERL, since it is designed as a 'back-end' language, and all the tricky things I know about have to do with PERL.

The RewriteMap program is started with Apache, and continues running, listening on STDIN, and writing responses to STDOUT. So you must be very careful with I/O buffering -- that is, don't use it. And of course, all per-request variables must be re-initialized for each new request.

You might also consider trying the intermediate step: Use a flat text file as a RewriteMap and see if that works; If so, you'll know it's a RewriteMap script problem. While that *is* the likely answer, it would still be good to know for sure before trying to debug the wrong thing.

Jim

uncoke

11:34 pm on Mar 12, 2008 (gmt 0)

10+ Year Member



Sorry, i was talking about my problem [post 1]

I think the rule forget therewritemap... prg but is it possible ?!?!

uncoke

12:39 am on Mar 13, 2008 (gmt 0)

10+ Year Member



Thank Morgan! Your note is very important!

But what do you mean writing "And of course, all per-request variables must be re-initialized for each new request" ? I think my problem is inside this...

knaito

2:44 am on May 15, 2008 (gmt 0)

10+ Year Member



Hi

I think that your script operates only one time.
However, in RewriteMap, the script should keep observing STDIN.

For example,

#!/usr/bin/perl
$¦ = 1;
while (<STDIN>) {
print $_;
}

in perl script.
The 'while loop' is necessary for keep observing STDIN.

asqpk

1:50 pm on Jun 25, 2008 (gmt 0)

10+ Year Member



Hi there
I am observing a malfunction while i am applying RewriteMap Perl and MySql solution

my httpd.conf script is :
RewriteEngine on
RewriteLogLevel 3
RewriteLog "/rewrite.log"
RewriteMap chmap prg:/check.pl
RewriteRule /sites.examples/(.*) ${chmap:$1} [NS,P]

and my perl file script is:

#!/usr/bin/perl

$¦=1;

# PERL MODULES
use Mysql;

# MYSQL CONFIG VARIABLES
$host = "localhost";
$database = "test";
$tablename = "rewrite";
$user = "#*$!xx";
$pw = "#*$!#*$!x";

# PERL CONNECT()
$connect = Mysql->connect($host, $database, $user, $pw);

# SELECT DB
$connect->selectdb($database);

#print 'http://192.168.1.24/website/sites/0162/examples@examples.com/s_31324654646464/index.html';

while (my $in = <STDIN>){

# $_ -- This holds the requested url to rewrite
chomp($in);

my $myquery = 0;
my $execute = 0;
my $array = 0;

# DEFINE A MySQL QUERY
$myquery = "SELECT * FROM rewrite WHERE short = \"$in\"";

# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($myquery);
@array = $execute->fetchrow;
print 'http://192.168.1.24/'.$array[1];

}
##EOF##

when i try to hit URL
[192.168.1.24...]

it never returns anything and keep trying to load

in log file i found this
192.168.1.10 - - [25/Jun/2008:22:08:12 +0500] [192.168.1.24/sid#877f398][rid#8b2bfc0/initial] (2) init rewrite engine with requested uri /sites.examples/helloworld
192.168.1.10 - - [25/Jun/2008:22:08:12 +0500] [192.168.1.24/sid#877f398][rid#8b2bfc0/initial] (3) applying pattern '/sites.examples/(.*)' to uri '/sites.examples/helloworld'

browser keep trying to load but nothing came up

on the other hand when i run this perl script on shell it runs well takes input and print the path and then listen for another input

could anyone please help me in this regard

Thanks