Forum Moderators: coopster

Message Too Old, No Replies

trouble changing spaces to hyphens in URL

htaccess and php to change url and links

         

sasori

10:36 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



Hello,
I'm stuck with a smarty based cart that uses php and htaccess to change dynamic urls to 'flat' urls. It also replaces spaces with underscores. I want to switch it to hyphens, but am running into trouble:

---------------------------
<?
for($i = 0; $i<strlen($title); $i++){
$c = $title[$i];
if(ereg("[0-9a-zA-Z]", $title[$i])){
$_title = $_title.$c;
}
else{
$_title = $_title."_";
// trying to change _ to - in title
}
$_title = str_replace("__", "_", $_title);
}
?>
----------------------------
htaccess:
RewriteRule^ladies-([0-9a-zA-Z\_\-]*)\.htm([l]?)$cart.php?p=product&product_code=$1 [L]
RewriteRule^([0-9a-zA-Z\_\-]*)\.htm([l]?)$cart.php?p=catalog&catalog_code=$1 [L]
RewriteRule^pages/([0-9a-zA-Z\_\-]*)\.htm([l]?)$cart.php?p=page&page_id=$1 [L]
---------------------------
I've tried swapping the _ with - in both files but then the resultant links with hyphens are not pulling up the page; I get a page missing error.

Can you figure out what I'm doing wrong?

Thanks so much!

eelixduppy

1:00 am on Aug 7, 2009 (gmt 0)



Do you want to change these characters in the Rewrite or with the PHP?

sasori

3:57 am on Aug 7, 2009 (gmt 0)

10+ Year Member



I think I have to do both. for example, if I change the php so it generates - instead of _, the links come out that way, but when clicked, the pages are not found.

so, I'm missing something in the htaccess... I think.

idfer

4:49 am on Aug 7, 2009 (gmt 0)

10+ Year Member



It looks like your rewrite regex already accounts for hyphens in the URL. If you can, turn on rewriting logging, level 2 should be enough, and see what you get:

RewriteEngine On
RewriteLog "path_to_apache/logs/rewrite.log"
RewriteLogLevel 2

Also your access and error logs might give you some clues. But i'm thinking you also need to change something in cart.php because it's expecting product_code etc. to have underscores in them not hyphens?

BTW, that's quite a code you got there for this:

$_title = preg_replace('{[^0-9-a-zA-Z]+}', '-', $title);

:)