Forums / Cotonti / Support / mod_rewrite HELP needed

url rewriting in .htaccess to create SEO subdomains and subfolders

GHengeveld
#23670 2010-03-19 20:39
Simplest solution is to allow any subdomain and any subfolder (subcat) to be set when calling a page, regardless of whether or not that page resides in that category. You can control that with the Cotonti rewrite.

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule . - [S=7]
RewriteRule ^/$ http://www.example.com/list.php?c=%1 [NC,QSA]
RewriteRule ^([a-z0-9_-]+)/$ http://www.example.com/list.php?c=$1 [NC,QSA]
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/$ http://www.example.com/list.php?c=$2 [NC,QSA]
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)/$ http://www.example.com/list.php?c=$3 [NC,QSA]
RewriteRule ^([a-z0-9_-]+)\.html$ http://www.example.com/page.php?al=$1 [NC,QSA]
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)\.html$ http://www.example.com/page.php?al=$2 [NC,QSA]
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)\.html$ http://www.example.com/page.php?al=$3 [NC,QSA]

This should:
- Rewrite main cat from subdomain to list.php
- Rewrite subcats from subfolder to list.php (up to 3 levels)
- Rewrite .html files to page.php?al= (up to 3 levels)

Note that I've included the full domain each time, to make sure calls are made to the www subdomain and not the fake (category) subdomain (where your files aren't located). You can leave the domain out if you've set a * CNAME example.com in your DNS records.
Also note that subfolders (categories) have to end with a slash. Page aliases and category codes may only consist of alphanumeric chars, - and _