title: mod_rewrite - how to redirect a mistyped 'pretty' url
body: <p>I have a site written in php which creates 'pretty' urls for each item (on category and search pages) like this,</p>
  
  <pre><code>mysite.com/category/slug-for-item-one/1
  mysite.com/category/slug-for-item-two/2
  </code></pre>
  
  <p>The /category/ and /slug/ is dependent upon the numeric id of the item</p>
  
  <p>I have mod_rewrite serve the actual content from urls like this:</p>
  
  <pre><code>mysite.com/view-item.php?id=1
  mysite.com/view-item.php?id=2
  </code></pre>
  
  <p>The content for each item is retrieved using just the items id.</p>
  
  <p>Here's my htaccess:</p>
  
  <pre><code>RewriteEngine on
  RewriteRule ^([^/\.]+)/?$ view-item.php?id=$1 [L]
  RewriteRule ^([^/\.]+)/([^/\.]+)/?$ view-item.php?pid=$2 [L]
  RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ view-item.php?id=$3 [L]
  </code></pre>
  
  <p>Everythings ok so far but, if someone lands on on a url like,</p>
  
  <pre><code>mysite.com/1
  mysite.com/catey/slug-for-item-one/1 
  </code></pre>
  
  <p>or  </p>
  
  <pre><code>mysite.com/category/slug-item-one/1
  </code></pre>
  
  <p>the content is still served, but how can I automatically reset or redirect to the canonical version of the url, to: </p>
  
  <pre><code>mysite.com/category/slug-for-item-one/1
  </code></pre>
  
  <p>I've searched SO and google extensively for an answer, but no luck. I've only used mod_rewrite for simple redirects such as from without www. to with www. and my understanding is tentative thus I'm struggling to understand how to proceed at the moment.</p>
  
  <p>Can anyone point me in the right direction? </p>
  
  <p>Thank you everyone for your help. Much appreciated. I'm working on an implementation of Jon Lin's answer as I'm more familiar with using php/mysql databases and understand how and why it should work. I aim to be done by Friday and will update this page when finished. Many thanks, Karl.   </p>
  
  <p><strong>* UPDATE *</strong>
  I have implemented Jon Lin's answer and now my 'pretty' urls, when mistyped are now redirected to the correct or 'canonical' url just as on SO. Thank you Jon and everyone who contributed!</p>