Monday, February 21, 2011

Mod rewrite with 3 parameters ?

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

From stackoverflow
  • 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.

    Sandeep Bansal : +1 you cannot use mod rewrite to rewrite a url without a query.
    David : Any modrewrite code to redirect everything after domain name to handler.php?everything=XXXX ? thanks
    Gumbo : @Sandeep Bansal: That’s just plain wrong. You sure can use mod_rewrite to rewrite every request without touching the query.
  • 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.

  • 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]
    

0 comments:

Post a Comment