Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.