Solved

Mod Rewrite Regex Syntax

Posted on 2008-10-12
1
133 Views
Last Modified: 2010-03-04
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)

/TMP/Cat1/2
/TMP/Cat1/2/3
/TMP/Cat1/2/3/4

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




0
Comment
Question by:Bob-Villa
1 Comment
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
ID: 22700949
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

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now