Michael Fever
Michael Fever

Reputation: 865

ModRewrite - How to configure .htaccess for mod_rewrite for SEO purposes

I'm trying to make some improvements to my e-commerce site to have better Search Engine Optimization. One of the more important things is to setup the product to not use the old:

/detail.php?pid=367

Instead we have setup 8 top level categories with 3 sub categories below each of the 8 top level cats.

So for example, one of those categories would look like thisL

/safety-cabinets-fluid-handling/fluid-drum-carts/

Now what I would like to do is have folders below these categories for each product within. Such as:

/safety-cabinets-fluid-handling/fluid-drum-carts/Lubrigard-Handling-Cart/

My thinking is probably that the best way would be to include the product ID at the end, so it would be this:

/safety-cabinets-fluid-handling/fluid-drum-carts/Lubrigard-Handling-Cart/856

Does this make sense? And what would I put in the .htaccess in this situation?

Upvotes: 1

Views: 249

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

You can have any URL structure as long as the pattern is found in the ID, not in the URL structure, which is the normal method.

In your question, the pattern could be the fact that the ID is numeric, so it can be captured from any URL that holds it, regardless of it's structure. For example:

http://example.com/cat1/cat2/ID/cat3 or

http://example.com/cat2/ID/cat3/cat1

makes no difference for a rule that captures a numeric string in any position, like this one:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  /([\d]+)/? 
RewriteRule .*  detail.php?pid=%1  [L,QSA]

It will pass silently the ID in the incoming URL to the script, like this:

http://example.com/detail.php?pid=ID

Of course, there can't be any other numeric string before the ID in the URL.

However, if you still want the ID to be fixed in the last position in the URL, just replace the above rule set with this one:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([0-9]+)/?$
RewriteRule .*  detail.php?pid=%1  [L,QSA]

Upvotes: 1

Related Questions