PHP site / htaccess for clean URLs / sort by color/price etc issue

Associate
Joined
15 Feb 2006
Posts
1,872
Location
hell
Hi

I'm trying to build a php driven site with rewritten URLs. I have the done the basics and have rewritten URLs working on my site.

The problem I now have is that I want to sort my data, and since you have to use the query string to do this, you end up having a very large htaccess file (which you have to write manually).

Example

I have a page listing 9 products out of 100 products.

I have a sorting navigation where users can select products by Gender, Type, Price, Colour. Users can also press "View All" to see all 100 products.

The URL that displays 9 products is:

www.domain.com/product

When the user clicks "Mens" from the sorting navigation, the URL changes to www.domain.com/product/gender/mens and my htaccess file instructs that "mens" should be considered part of the gender variable in the query string.

Now if on the mens product page, they press "color = red" the URL changes to

www.domain.com/product/gender/mens/color/red and htaccess file instructs that color variable should be red.

My PHP page has the relevant queries set up that see if color/gender etc is set().

now the issue is, to my (basic) knowledge, you have to rewrite regular expressions in the correct order. So for example i have to have both: color/([^/]+)/gender([^/]+) and gender([^/]+)/color([^/]+) set up in my htaccess to drop the words in the correct variables.

This gets extremely complex if you throw other sorts into the mix - depending on the user journey, the URL is different

www.domain.com/color/red/gender/ladies/speed/fast
www.domain.com/speed/fast/color/red/gender/ladies
www.domain.com/gender/ladies/color/red/speed/fast etc etc all have to be accounted for in the htaccess.

BTW
I'm using a script that takes the current URL of my page, and appends the relevant /gender etc onto the end for the creation of links on my page.

Can anyone assist me with this dilema?

I want to make the sort work like www.asos.com (clothes shopping site). YOu can sort by various things - they don't use rewritten URLs though so it's a LOT easier?
 
Personally I'd rather sort stuff using SQL queries.

So if your SQL table has a colour/gender/speed fields, then you can do:
PHP:
mysql_query("SELECT color/gender/speed FROM TableName ORDER BY color/gender/speed");
 
Hi

Yes I will be using queries to do the actual sort, but I'm talking about the bit before the sort.

e.g.

IF you want products that are red, and below 100 pounds:

Click Red, Click 100 pounds - URL then = www.domain.com/product/colour/red/price/sub100

then I use the htaccess to assign $color="red" and price="sub100"; and place these into my queries.

My problem is that you need a specific order in the .htaccess, so if they click in the reverse order (price, then color) i'll need a rule for that too
 
Hmm sounds like you're weaving yourself a bit of a htaccess headache, I'd use dummy values that the system can ignore so you have just one pattern of address for your search ie

domain.com/color/all/gender/ladies/speed/all/price/all/
domain.com/color/red/gender/all/speed/all/price/all/
domain.com/color/all/gender/all/speed/fast/price/all/
domain.com/color/all/gender/all/speed/all/price/sub100/
 
Yeah for a search system but usually it's done with querystrings and you often see horrible addresses full of values=0. At least this way there's a tangible address that people can forward and save etc
 
I would have the PHP doing the hard work rather than the htaccess.

Have a pretty simple htaccess which redirects

www.domain.com/product/gender/mens/color/red

to

search.php?query1=gender&value1=mens&query2=color&value2=red

i.e. the first value to query1, the second to value1, the third to query2, the fourth to value2.

Then use your PHP to clean up the input and write the SQL query dynamically, e.g.

Code:
$x = 1;
while((isset($_REQUEST['query' . $x])) && (isset($_REQUEST['value' . $x]))) {
  switch($_REQUEST['query' . $x]) {
    case 'gender':
    default:
      $sort[$x]['query'] = 'gender';
      break;
      
    case 'size':
      $sort[$x]['query'] = 'size';
      break;
      
    case 'speed':
      $sort[$x]['query'] = 'speed';
      break;
      
  }
  
  $sort[$x]['value'] = mysql_real_escape_string($_REQUEST['value' . $x]);
  $x++;

}
Now you have a two-dimensional array containing pairs of values - the sort mode (gender/color etc.) and the sort query (mens/red etc.) You can use this array to write your SQL query, e.g.

Code:
foreach ($sort as $query => $value) {
 
// do stuff here to construct the SQL

}
Someone more experienced might shoot me down, but I can't see why that wouldn't work.
 
Last edited:
I would set it up, so that

www.domain.com/product/gender/mens/color/red

goes to something like

search.php?query1=gender&type1=mens&query2=color&type2=red

...then all you'd need is a pretty simple htaccess to set the first pair of values to query1 and type1, and to repeat this for as many pairs as were in the URL.

Then you use your PHP to do the hard work, constructing the SQL query based on the input that's received, e.g.

Code:
if(isset($_REQUEST['query1'])) {
  switch ($_REQUEST['query1']) {
    case 'gender':
    $query1 = 'gender';[/QUOTE]

hmm I can kinda see what you are saying.  

So by having it rewrite to  query=$1 instead of gender=$1 it doesn't matter what the first input is?

as the PHP would just take whatever is query 1 and compare using switch case like you said and set the variables accordingly

I guess this means i'll have lines in my htaccess for:

query1/type1
query1/type1/query2/type2
query1/type1/query2/type2/query3/type3 and so on depending on the amount of filters i have?
 
Sorry, my post submitted before I was finished!

Scroll up - I've edited it. (Notice I changed it to cope with any number of query pairs.) I've only been doing this a few months so check with someone more experienced than me before you use it, but I can't see why it wouldn't work.
 
Last edited:
Yeah I think that would work pretty well.

If you're going to retain the category details then that would be essential to run the system that way. Personally I'd try and shorten the URLs so they don't include the category titles and just the info but then you'd definitely need to keep a single structure ie

domain.com/red/all/all/all/
domain.com/red/all/all/sub100/
domain.com/red/all/fast/sub100/
domain.com/red/ladies/fast/sub100/

Be aware, if you're specifically using this system to help with SEO, Google will penalise you for finding duplicate content at multiple addresses on your site, which is possible with your proposed structure.
 
Back
Top Bottom