SiobhanElara
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=a bout > 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...
...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!
testsite.com/index.cfm?p=a
BUT ALSO
testsite.com/subdirectory/
I can do the first with the following code...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.cfm?p=$1 [NC,L]
...but this will send testsite.com/subdirectory/
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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]
ASKER
There we go. Thanks so much!
ASKER