views:

222

answers:

6

I have problems with how I should structure my product listing pages, products pages, webpages.

It roughly translate into this:

  1. /bags/nicebag.html = /product.php?product=nicebag&category=bags
  2. /nicebag.html = /product.php?product=nice_bag
  3. /bags = productlisting.php?&category=bags

Problem is that webpages will share same URL structure as no.2 in the list /contact.html = page.php?page=contact

The reason why it is not listed in .htaccess separatly is that webpages can have different names. And even the same page can be in multiple languages.

The reason of no. 1 and 2 is not combined, is that sometimes I just want to reference only to the product since it can be in multiple categories.

What kind of structure do you suggest?

.htaccess

# Mod rewrite enabled.
Options +FollowSymLinks
RewriteEngine on

# ---- Rules ----

# product.php (Search for category & product name)
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)\.html?$ product.php?prod_id=$2&cid=$1 [NC,L]

# productlisting.php (Search for category)
RewriteRule ^([A-Za-z0-9-_]+)?$ productlisting.php?&cid=$1 [NC,L]
A: 

I was facing the very same issue a few weeks ago. Ended up defining a different structure for the "static" pages.

www.examples.com/contact/ or www.examples.com/info/contact.html

So it can be distinguished from the "dynamic" pages.

Fabian
Was thinking the same. But I feels a bit clumsy to do it that way. But maybe I have to do that :(
Cudos
A: 

There's pretty much no way to distinguish between www.examples.com/nicebag.html and www.examples.com/contact.html without putting non-product webpage names in .htaccess or doing some preliminary processing in the receiving php script.

As I see, the options are:

  1. rewrite all requests to page.php and for those that don't match any of the non-product pages, include the product script

  2. write the non-product page names to .htaccess dynamically (messy and bug-prone)

  3. rethink the URL structure for non-product pages. Perhaps just as little as www.example.com/page/contact.html might help

I'd go for the third one, anyway.

Nouveau
+2  A: 

I would use the path prefix /products/ to identify the products related URLs. So:

  • /products/bags/nicebag.html/product.php?product=nicebag&category=bags
  • /products/nicebag.html/product.php?product=nice_bag
  • /products/bags/productlisting.php?&category=bags

With such a structure you could also rewrite /products/ to /productlisting.php that then shows all products.

# product listing
RewriteRule ^products/$ productlisting.php [L]
RewriteRule ^products/([A-Za-z0-9-_]+])$ productlisting.php?category=$1 [L]
# product details
RewriteRule ^products/([A-Za-z0-9-_]+)\.html$ product.php?prod_id=$1 [L]
RewriteRule ^products/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)\.html$ product.php?prod_id=$2&cid=$1 [L]
# other pages
RewriteRule ^([A-Za-z0-9-_]+)\.html$ page.php?page=$1 [L]
Gumbo
The idea is to avoid having "extra" keywords in the URL. Because that will maybe have some unforseen negative effects for the Search Optimization in Google.
Cudos
A: 

I would recommend flat structure:

  • domain.com/bags
  • domain.com/contact
  • domain.com/nice-bag
dusoft
+1  A: 

As it will be messy and cumbersome to maintain your rewriting rules in the .htaccess file, I would only put one rule in there, rewriting to something like:

/dispatch.php?request=[request]

e.g.

RewriteRule ^(.*)$ dispatch.php?request=$1 [L,QSA]

In dispatch.php, you dissect the request into its elements (path, querystring, anchor, ...) and decide where to go from there. That way, you can use code for the decision making, which will give you a lot more flexibility than just maintaining a huge list of custom rewrite mappings.

For example, you can identify product and category elements in the path by querying against your database and base the dispatch logic on the results in a more generic way.

[Pseudocode]
if (isProduct($lastPathElement)) {
  // Maybe verify that leading path elements are categories ...
  // Other preparations/verifications ...
  // refer execution to product.php
}
elseif (isCategory($lastPathElement)) {
  // Other preparations/verifications ...
  // refer execution to productlisting.php
}
// ... (Checks for other specific stuff)
else {
  // Static page or 404
  // refer execution to page.php
}
Henrik Opel
This one is very good! But my existing code doesn't allow for this. I should have designed my code from this point of view first then.
Cudos
+1  A: 

use a different suffix for different types, e.g html for products and htm for pages or something like that

/bags/nicebag.html = /product.php?product=nicebag&category=bags
/nicebag.html = /product.php?product=nice_bag
/bags = productlisting.php?&category=bags
/contact.htm = page.php?page=contact

or

/contact/page.html = page.php?page=contact
Nir Levy
That was my thought too. I will go with that.
Cudos
cheers, have a ball
Nir Levy