I've got a site designed in php which produces 'pretty' web addresses for every item (on category and check pages) such as this,


The /category/ and /slug/ depends upon the number id from the item

I've mod_rewrite serve the particular content from web addresses such as this:


This content for every item is retrieved with the products id.

Here's my htaccess:

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ view-item.php?id=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ view-item.php?pid=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ view-item.php?id=$3 [L]

Everythings ok to date but, if a person arrives at on the url like,




this content continues to be offered, but how do i instantly totally reset or redirect towards the canonical version from the url, to:


I have looked SO and google extensively to have an answer, but no luck. I have only used mod_rewrite for straightforward redirects for example from without www. to with www. and my understanding is tentative thus I am battling to learn how to proceed right now.

Can anybody point me within the right direction?

Thanks everybody for the help. Much appreciated. I am focusing on an implementation of Jon Lin's answer as I am more acquainted with using php/mysql databases and understand why and how it will work. I goal to be carried out by Friday and can update this site when finished. Thank you, Karl.

* UPDATE * I've implemented Jon Lin's answer and today my 'pretty' web addresses, when mistyped are actually rerouted towards the correct or 'canonical' url just like on SO. Thanks Jon and everybody who led!

I believe you will need two teams of rewrite rules to do this.

The very first algorithm would be employed to send 301 redirects towards the client to be referencing the canonical Web addresses:

RewriteRule ^/1    /category/slug-for-item-one/1 [R=301,L]

A second algorithm which use passthroughs [PT] for everyone in the content:

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ view-item.php?id=$3 [PT,L]

Or something like that along individuals lines...