views:

76

answers:

2

Hello,

I want to make a category path with pagination. URL's would be as followed;

All paths should work with or without trailing slash (if possible)

/category/entertainment-and-music/

/category/entertainment-and-music/music/

(represents "music" category "under entertainment and music")

/category/entertainment-and-music/5/

(represents 5th page for "ent. and music")

/category/entertainment-and-music/music/5/

(represents 5th page for "ent. and music/music")

I tried something like that but doesn't work

RewriteRule ^category/(.*)/? /category.php?slug=$1&page=1
RewriteRule ^category/(.*)/(.*)/? /category.php?slug=$1&page=$2

How can I do something flexible like that?

Thanks in advance...

+1  A: 

It looks like your first regex also matches anything that would be caught by your second one.

/category/slugname/7/ would match the .* , with $1 set to "slugname/7"

Try something like this:

RewriteRule ^category/([^/]*)/? /category.php?slug=$1&page=1
RewriteRule ^category/([^/]*)/(.*)/? /category.php?slug=$1&page=$2

At least, or tighten up your matches a bit, like this:

RewriteRule ^category/([a-zA-Z0-9_-]+)/? /category.php?slug=$1&page=1
RewriteRule ^category/([a-zA-Z0-9_-]+)/([0-9]+)/? /category.php?slug=$1&page=$2

That would constrain your slugs to be at least one alphanumeric character, allowing "_" and "-" as well, and your page numbers would have to be, well, numbers.

Ian Clelland
Thank you very much but unfortunetely doesn't work, it returns only first part of slug, "entertainment and music". No sub-category or page number...
she hates me
Two possibilities: First, perhaps the regexes should have a '$' at the end of them (after the ?), so that the string must end at that point -- otherwise, the first regex is still matching everything. Second, if that doesn't work, try switching the order of the two rules, so that the more specific rule matches first.
Ian Clelland
I tried but still doesn't work :( Doesn't get sub category...
she hates me
+2  A: 
RewriteRule ^category/([A-Za-z0-9_-]+)([/]?)$ /category.php?slug=$1&page=1
RewriteRule ^category/([A-Za-z0-9_-]+)/([0-9]+)([/]?)$ /category.php?slug=$1&page=$2
RewriteRule ^category/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([0-9]+)([/]?)$ /category.php?slug=$1&subcat=$2&page=$3

First rule should cover cases where they just specify a category.

Second rule should cover the category with a page supplied.

Third rule should cover category, sub-category, and page. I wasn't real clear on how you wanted the subcategory populated so I just wrote it out as another argument after slug.

All of them allow for an optional trailing slash.