Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite() and php

how to change space in hyphen in a var whit 2 words

         

lixor

1:37 pm on May 28, 2005 (gmt 0)

10+ Year Member



hi

i have only one file phones.php. when i have phones.php i show all phones manufacturers when i have phones.php?mn=alcatel i show all alcatel's phones and when i have phones.php?mn=alcatel&pn=ot156 i show all about this phones (alcatel ot156).

now i have this .htaccess file:

RewriteEngine on

RewriteRule ^phones.htm$ /phones.php [L]
RewriteRule ^phones/(.*)\.htm$ /phones.php?mn=$1 [L]
RewriteRule ^phones/(.*)/(.*)\.htm$ /phones.php?mn=$1&pn=$2 [L]

1) why if i tape w*w.site.com/phones/alcatel/ot156.htm and in script i have echo $_GET['mn']." ".$_GET['pn'] it will echo alcatel\ot156 and not alcatel ot156
2) and my big problem how do i manage with this w*w.site.com/phones/sony%20ericsson/k%20500i.htm or w*w.site.com/phones/sony+ericsson/k+500i.htm. so when i have a variable composed from 2 or 3 words how do i change the space or + sign(from urlencode) in hyphen.
i want to have w*w.site.com/phones/sony-ericsson/k-500i.htm and so on for the others. what i should modify in my .htaccess file.

thanks

jd01

6:17 pm on May 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi lixor,

The problem appears to be that special characters, including (space), are encoded by Apache and your are getting the output from that.

The best solution I have found is to change the characters in the links to product-stuff (you may need to use a regex in your php), then use a (or another) php regular expression to change back to product stuff at the beginning of the page that is opening.

Your .htaccess looks fine for this.

Hope this helps.

Justin

BTW I think it is much more practical to use PHP than Apache for this, that's why my answer is the way it is... The only way I can think to do it in Apache, requires two rewrites and is much more complicated and server intensive. Maybe someone else has a simple way to do this in Apache, but I can't think of one.

jdMorgan

6:26 pm on May 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lixor,

Welcome to WebmasterWorld!

I agree with jd01 -- The proper place to do most of the work is in php.

But while we're here, let's clear up a problem with the mod_rewrite code, which was caused by using the greedy and too-ambiguous ".*" pattern:


RewriteRule ^phones.htm$ /phones.php [L]
RewriteRule ^phones/([^.]+)\.htm$ /phones.php?mn=$1 [L]
RewriteRule ^phones/([^/]+)/([^.]+)\.htm$ /phones.php?mn=$1&pn=$2 [L]

Here, we use patterns that look for a specific 'ending' character for each variable-field in the requested URL -- either a "." or a "/", as required. This makes the patterns much more specific.

Again, ".*" is an easy pattern to use, but it should only be used when there is no other pattern that will work. It is greedy, promiscuous, and therefore inefficient.

The original code could probably be made to work by reversing the rule order, but the fundamental problem is that the RewriteRule patterns were ambiguous.

Jim

lixor

11:16 am on May 29, 2005 (gmt 0)

10+ Year Member



glad to be on this forum. it works just perfect, but you know, i've changed the order:

RewriteRule ^phones/([^/]+)/([^.]+)\.htm$ /phones.php?mn=$1&pn=$2 [L]
RewriteRule ^phones/([^.]+)\.htm$ /phones.php?mn=$1 [L]
RewriteRule ^phones.htm$ /phones.php [L]

but why, if i rename my file from phones.php to cell-phones.php and i change the .htaccess properly it doesn't work. i must try to escape anything (the hyphens) in the .htaccess file?!

thanks

jd01

9:24 pm on May 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...it doesn't work

How, specifically does it not work?

500 error?
404 error?
No rewrite?

Without knowing this there is no way of telling what is wrong for sure... You do not, and should not, have to escape the - on the right side of the rule.

Justin

lixor

9:54 am on May 30, 2005 (gmt 0)

10+ Year Member



ok my mistake.

i have: cellphones.php


<?php
if (isset($_GET['mn'])) {
echo $_GET['mn'];
}else{
echo "No cell phone selected";
}
?>

and .htaccess

RewriteEngine on

RewriteRule ^cellphones/([^.]+)\.htm$ /cellphones.php?mn=$1 [L]
RewriteRule ^cellphones.htm$ /cellphones.php [L]

when i type h**p://localhost/cellphones.htm it will display No phone selected and for h**p://localhost/cellphones/alcatel.htm alcatel.

but if i change the name to cell-phones.php and in .htaccess i put

RewriteEngine on

RewriteRule ^cell-phones/([^.]+)\.htm$ /cell-phones.php?mn=$1 [L]
RewriteRule ^cell-phones.htm$ /cell-phones.php [L]

it will display No phone selected for both cases h**p://localhost/cell-phones.htm and h**p://localhost/cell-phones/alcatel.htm

i test local on apache 1.3.23, php 5.0.2 and windows xp.

lixor

3:11 pm on May 31, 2005 (gmt 0)

10+ Year Member



i uploaded the files on server and it works just fine. i think is for windows not working with cell-phones.php in my computer. thanks