Forum Moderators: phranque
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
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.
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]
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
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
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.