Link to home
Start Free TrialLog in
Avatar of SiobhanElara
SiobhanElaraFlag for United States of America

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SiobhanElara

ASKER

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

Open in new window

There we go. Thanks so much!