Mod Rewrite Regex Syntax

Here's a sample of some code from my .htaccess file

RewriteRule ^TMP/Cat(.*)/(.*)$ /TMP/run.php?r=cat$1&c1=$2

How can I get this to return valid if the user types a url with ot without a trailing slash?
with my above example  /TMP/Cat1/2  works but  /TMP/Cat1/2/  does not

RewriteRule ^TMP/Cat(.*)/(.*)/(.*)/(.*)$ /TMP/run.php?r=cat$1&c1=$2&c2=$3&o=$4
RewriteRule ^TMP/Cat(.*)/(.*)/(.*)$ /TMP/run.php?r=cat$1&c1=$2&o=$3
RewriteRule ^TMP/Cat(.*)/(.*)$ /TMP/run.php?r=cat$1&c1=$2

Anyway to consolidate this?

I don't want to have to add filler to my URLS.

i.e.  I want the following urls to work with one entry in my htaccess file if that is possible: (keep in mind my first question)


I don't want to have to write a url like this:

/TMP/Cat1/2/0/0  then have my script ignore the zeros as a workaround

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

caterham_wwwConnect With a Mentor Commented:
You can make a / optional by adding a '?' --> /?
RewriteRule ^TMP/Cat([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /TMP/run.php?r=cat$1&c1=$2&c2=$3&o=$4 [L]
RewriteRule ^TMP/Cat([^/]+)/([^/]+)/([^/]+)/?$ /TMP/run.php?r=cat$1&c1=$2&o=$3 [L]
RewriteRule ^TMP/Cat([^/]+)/([^/]+)/?$ /TMP/run.php?r=cat$1&c1=$2 [L]

Open in new window

All Courses

From novice to tech pro — start learning today.