How can I have one .htaccess file handle SEO-friendly URLs for root and subdirectories?

I'm trying to create a single .htaccess file that makes seo-friendly URL's for the root directory AND subdirectories. For example:

testsite.com/index.cfm?p=about > testsite.com/about

BUT ALSO

testsite.com/subdirectory/index.cfm?p=widgets > testsite.com/subdirectory/widgets

I can do the first with the following code...

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.cfm?p=$1 [NC,L]

Open in new window


...but this will send testsite.com/subdirectory/index.cfm?p=widgets to testsite.com/widgets

Can you help me with redirecting subdirectories properly? Everything I've found on Google just shows you how to redirect specific subdirectories, not how to capture the subdirectory you're currently in. Thanks!
SiobhanElaraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve BinkCommented:
Try something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)/([^/]+)$ /$1/index.cfm?p=$2 [NC,L]

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SiobhanElaraAuthor Commented:
Perfect, thank you!
SiobhanElaraAuthor Commented:
Actually, I spoke too soon;! This works for the second (with subdirectories) but not for the first (from the root.)
Steve BinkCommented:
Try this instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+/)?([^/]+)$ /$1index.cfm?p=$2 [NC,L]

Open in new window

SiobhanElaraAuthor Commented:
There we go. Thanks so much!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.