.htaccess friendly urls

software development

#1

I have a dynamic website, and I’m trying to change this:

http://www.bundoranpress.com/03product.php?category_id=14&product_id=13

Into something friendlier like this:

http://www.bundoranpress.com/14/13/ (I’ll switch the numbers later)

Anyhow, this is what I’m using but I’m having no luck (I’ve even used htaccess generators):

RewriteEngine On
RewriteRule /(.)/(.)/$ 03product.php?category_id=$1&product_id=$2

But I’m getting nothing…no changes whatsoever. I’ve tested my htaccess page using a very simple test (and my htaccess is active) but in this regard…can’t seem to get anywhere.


#2

Ok, first the RewriteRule is saying match all lines with slash /, any character zero or more times (.), slash /, any character zero or more times (.)

Your supplied Url doesn’t have that in it so you won’t match. Everything kind of works backwards in mod_rewrite

Warning untested code below :slight_smile:
What you want is something like:

RewriteCond %{REQUEST_URI} ^/03product.php
RewriteCond %{QUERY_STRING} category_id=([0-9]+)
RewriteCond %{QUERY_STRING} product_id=([0-9]+)
RewriteRule .* 03product.php/$1/$2

Jw


#3

RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/([0-9]+)$ 03product.php?category_id=$1&product_id=$2 [L]

I’ve linked to the reference documentation for you.


#4

#5

Try changing the regexp to

^([0-9]+)/([0-9]+)/$

to catch /14/13/

And

^([0-9]+)/([0-9]+)/?$

will catch either /14/13 or /14/13/


#6

Awesome. That works great. I’m not familiar with reg expressions (if that’s what they’re called). I’ve changed it a little bit so it will now do that with the names, rather than just numbers. However…

Is there a way to substitute spaces in strings? So instead of this: product/02/a%20book%20I%20love I’d like product/02/a-book-I-love (for instance)


#7

You would need a RewriteRule with the “next” flag to substitute characters.