spacepoet Posted February 25, 2012 Share Posted February 25, 2012 Hello: I'm doing m first mod_rewrite and it is mostly going well. But, I have one issue I can't see to get right. The products I'm entering in the database have spaces, and I want to replace them with hyphens in the .htaccess file to match what I have replaced in the PHP file. Otherwise, it is not working. This is my PHP file: <?php $query = "SELECT product_id,myTitle FROM myProducts ORDER BY myTitle ASC"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $product_id = stripslashes($row['product_id']); $myTitle = stripslashes($row['myTitle']); ?> <li><a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo str_replace(" ", "-", $myTitle) ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a></li> <?php } ?> This is the part in the .htacces file where it "matches" the PHP file: RewriteRule ^Promotional.Products-Promotional.Items/Promotional-Products/([0-9]+)-([a-zA-Z]+)-([a-zA-Z]+)-Promotional-Products.html$ Promotional.Products-Promotional.Items/Promotional-Products.php?product_id=$1&myTitle=$2&full_state=$3 (full_state is coming from a variable already on the page, BTW, and works fine) I think I need to somehow tell the middle RegEx ([a-zA-Z]+) to replace the spaces with hyphens. Everything works great otherwise. How can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/257783-replacing-spaces-with-hyphones-in-htaccess/ Share on other sites More sharing options...
requinix Posted February 26, 2012 Share Posted February 26, 2012 mod_rewrite cannot do substitutions like that. Technically it can, but it's absolutely ridiculous and should not be done. Quote Link to comment https://forums.phpfreaks.com/topic/257783-replacing-spaces-with-hyphones-in-htaccess/#findComment-1321337 Share on other sites More sharing options...
spacepoet Posted February 26, 2012 Author Share Posted February 26, 2012 Ok...thats a bummer ... however, is there a way to do the "string_replace" on $MyTitlt when it comrs out of the database and not in the href link? that might fix my problem but i cant seem to figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/257783-replacing-spaces-with-hyphones-in-htaccess/#findComment-1321535 Share on other sites More sharing options...
requinix Posted February 27, 2012 Share Posted February 27, 2012 You're already putting the ID number in the URL - you can identify the product precisely. Why does it matter if the name isn't exactly right? Quote Link to comment https://forums.phpfreaks.com/topic/257783-replacing-spaces-with-hyphones-in-htaccess/#findComment-1321639 Share on other sites More sharing options...
spacepoet Posted February 27, 2012 Author Share Posted February 27, 2012 Because the mod rewrite fails. i dont know why. so, any idea about the question i just posted or should i try another area of this forum. Quote Link to comment https://forums.phpfreaks.com/topic/257783-replacing-spaces-with-hyphones-in-htaccess/#findComment-1321723 Share on other sites More sharing options...
requinix Posted February 28, 2012 Share Posted February 28, 2012 What is the URL that is failing? Quote Link to comment https://forums.phpfreaks.com/topic/257783-replacing-spaces-with-hyphones-in-htaccess/#findComment-1321878 Share on other sites More sharing options...
spacepoet Posted February 28, 2012 Author Share Posted February 28, 2012 This one: <a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo str_replace(" ", "-", $myTitle) ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a> However, I believe I know how to fix it, but don't know how to revise the middle RegEx expression. ...-([a-zA-Z]+)-... How do I add "replace spaces with hyphens" to that? I know that is the key to fixing this. Sorry about all the posts, but this is driving me nuts until I figure it out. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/257783-replacing-spaces-with-hyphones-in-htaccess/#findComment-1322095 Share on other sites More sharing options...
requinix Posted February 29, 2012 Share Posted February 29, 2012 This one: --Promotional-Products.html" title=""> That's not a URL. That's some HTML with a bit of PHP code sprinkled in. What is the actual URL that is failing? The one that shows up in your browser's address bar. Quote Link to comment https://forums.phpfreaks.com/topic/257783-replacing-spaces-with-hyphones-in-htaccess/#findComment-1322211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.