Jump to content

Replacing spaces with hyphones in .htaccess


spacepoet

Recommended Posts

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?

 

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.