Question

I am in the process of converting my urls to friendly urls and I'm struggling a little bit with some of them,

For example Lets say posts and categories,

Post (detail):

/post/the-title/10 /* /post.php?id=10 */

Posts (list)

/posts/  /* /posts.php?what=last */
/posts/?what=top /* /posts.php?what=top */
...

But What I'm not so sure how to implement categories, in order to keep the structure of the urls, I would like to acomplish:

/posts/category-name/5 /* /posts.php?what=cat&id=5 */

But this is how I am re-rewriting my urls (for listing):

RewriteRule ^posts/$ posts.php?$1&friendly=1 [QSA]

So I believe I should preppend it so the other one doesn't trigger, something like:

RewriteRule ^posts/(.+)/(.+) posts.php?what=cat&id=$2&friendly=1
RewriteRule ^posts/$ posts.php?$1&friendly=1 [QSA]

So questions, here, are:

1) Is that a way (near) to go? (expecting that url types will change)
2) Will establishing the RewriteRule before the other one asure that it won't end up in a loop?

Any input would be welcome, I'd like to think this through before commiting to a url structure

Était-ce utile?

La solution

For "/post/the-title/10 /* /post.php?id=10 */"

RewriteRule ^post/[^/]+/([0-9]+)/?$ /post.php?id=$1&friendly=1 [L,QSA]

For "/posts/ /* /posts.php?what=last */"

RewriteCond %{QUERY_STRING} !^what=
RewriteRule ^posts/?$ posts.php?what=last&friendly=1 [L,QSA]

For "/posts/?what=top /* /posts.php?what=top */"

RewriteCond %{QUERY_STRING} ^what=
RewriteRule ^posts/?$ posts.php?friendly=1 [L,QSA]

For "/posts/category-name/5 /* /posts.php?what=cat&id=5 */"

RewriteRule ^posts/[^/]+/([0-9]+)/?$ /posts.php?what=cat&id=$1&friendly=1 [L,QSA]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top