views:

45

answers:

3

Hello, I did tons of methods to figure out how to make this mod rewrite but O was completly unsuccessful.

I want a .htaccess code that rewrite in the following method:

  • /apple/upcoming/2/handler.php?topic=apple&orderby=upcoming&page=2

This is easy to do, but the problem is that all parameters are not required so the link has different levels of parameters each time like this:

  • /apple/popular/2/handler.php?topic=apple&orderby=popular&page=2
  • /apple/2/handler.php?topic=apple&orderby=&page=2
  • /all/popular/2/handler.php?topic=all&orderby=popular&page=2
  • /apple/upcoming//handler.php?topic=apple&orderby=upcoming&page=

So briefly, the URL has 3 optional parameters in one static order: (topic) (orderby) (page)

Note: the ORDERBY parameter can be "popular" or "upcoming" or nothing.

Thanks

+1  A: 

i would suggest redirecting everything after domain name(/apple/upcoming/2) to index.php and from there use php to parse url and call appropriate function.

Funky Dude
+1 you cannot use mod rewrite to rewrite a url without a query.
Sandeep Bansal
Any modrewrite code to redirect everything after domain name to handler.php?everything=XXXX ? thanks
David
@Sandeep Bansal: That’s just plain wrong. You sure can use mod_rewrite to rewrite every request without touching the query.
Gumbo
+1  A: 

This should work:

RewriteRule     ^([0-9]*)/?$ handler.php?topic=&orderby=&page=$1 [L]
RewriteRule     ^(upcoming|popular)/([0-9]*)/?$ handler.php?topic=&orderby=$1&page=$2 [L]
RewriteRule     ^([^/]*)/([0-9]*)/?$ handler.php?topic=$1&orderby=&page=$2 [L]
RewriteRule     ^([^/]*)/(upcoming|popular)/?$ handler.php?topic=$1&orderby=$2&page= [L]
RewriteRule     ^([^/]*)/(upcoming|popular)/([0-9]*)/?$ handler.php?topic=$1&orderby=$2&page=$3 [L]

You should simply declare rewrites in preferred order.

dchekmarev
A: 

For the cases you provided above there rules should work:

RewriteRule     ^([^/]+)/(\d+)/?$ handler.php?topic=$1&orderby=&page=$2 [L]
RewriteRule     ^([^/]+)/([^/]+)/?$ handler.php?topic=$1&orderby=$2&page= [L]
RewriteRule     ^([^/]+)/([^/]+)/(\d+)/?$ handler.php?topic=$1&orderby=$2&page=$3 [L]
TonyCool