Correct .htaccess structure


by Php Ninza on August 2, 2008

Usually we all get very greedy while making our url structure using .htaccess

For example, This will not work as you think

RewriteRule ^(.*)/$ category/categories.php?cat=$1 [L,QSA]
RewriteRule ^(.*)/(.*)/$ category/categories.php?cat=$1&name=$2 [L,QSA]

This will not work as after first rewrite will write , the current url structure will change to www.mydomain.com/<something>, Which will result to the last rewrite rule to be implemented after current url structure , i.e www.mydomain.com/<something>/ , Which is different from origianl structure www.mydomain.com. This can also result in loss of images as they are on relative path

RewriteRule ^([^/]+)/$ category/categories.php?region=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/$ category/categories.php?cat=$1&name=$2 [L,QSA]

My two cents

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 2 comments… read them below or Shout @ me! }

Ben July 26, 2009 at 2:57 pm

Isn’t it better to use “RewriteRule ^(.*)$ controller.php?route=$1 [L,QSA]“, and then parse this $1 (which could look like “/category/7″) in index.php, MVC-style? Cleaner design, *and* better SEO.

At least, it’s how I tend to do it.

Ben July 26, 2009 at 2:59 pm

Uhm, s/index.php/controller.php/

Leave a Comment

{ 1 trackback }

Previous post:

Next post: