.htaccess排序和名称冲突

i have different url (setup in .htaccess) and now i want to add more features to it.

for example:

  • /section/
  • /section/1/
  • /section/3/name/

this will display the section page (a list of items) using the default sort, the second is the page number and the third and fourth are the sort by and order by. in this case, order by name desc by default.

now, i also want:

  • /section/
  • /section/1/
  • /section/3/product

which is the section, product id and product name (so i can query by id for efficiency)

i have so far:

RewriteEngine On
RewriteRule ^([^/]+)$ index.php?section=$1
RewriteRule ^([^/]+)/([0-9]+)$ index.php?section=$1&pagenum=$2
RewriteRule ^([^/]+)/([0-9]+)/([^/]+)$ index.php?section=$1&pagenum=$2&orderby$3

this works for one but not both, any recommendations?

Here's what you can do:

RewriteEngine On
RewriteBase /

You can use this for as common functionality:

RewriteRule ^([^/]+)$ index.php?section=$1 [L,NC]
RewriteRule ^([^/]+)/([0-9]+)$ index.php?section=$1&pagenum=$2 [L,NC]

By default, you don't want to display the product without a name (assuming you want SEO urls)

Now for the page specific:

RewriteRule ^([a-z0-9\-]+)/([0-9]+)/([a-z0-9\-]+)/?$ product.php?section=$1&id=$2&name=$3 [L,NC]

Notice the file name here. Because if you have 3 elments (section/id/name) means you want to display a product.

And now, the list/order specific

RewriteRule ^([a-z0-9\-]+)/([0-9]+)/([a-z0-9\-]+)/(asc|desc)/?$ list.php?section=$1&pagenum=$2&sortcolumn=$3&orderby=$4 [L,NC]

Notice also the filename here. If you add a desc or asc at the end of the URL it means you will display the list of products. This last parameters will allow to switch between products and product list. Of course you have to use the asc or desc keyword all the time. The other alternative you can use is to use the word list or any other relevant name to display the list example:

RewriteRule ^list/([a-z0-9\-]+)/([0-9]+)/([a-z0-9\-]+)/(asc|desc)?$ list.php?section=$1&pagenum=$2&sortcolumn=$3&orderby=$4 [L,NC]

In this case, all rules that starts with list will display the list of products and not the product itself