rlelek Posted June 24, 2009 Share Posted June 24, 2009 Hello everyone. I've been trying my best to figure out the best way to introduce some logic to mod_rewrite. Right now, the RewriteRules are *very* repetitive, even with regex's. Here's what I'm looking at... RewriteRule ^movies/genres/([a-zA-Z][^/]+)?/([^/.]+)?/?(page)?/?([^/.]+)?$ /browse.php?type=1&option=genre&option_value=$2&page=$4 [L] Basically, the URL will look like this: http://www.example.com/movies/genres/Comedy/12/page/3 where - Comedy is there for SEO (no pass to PHP) - the '12' is passed to PHP to represent Comedy (genre id) - the '3' is the page number Sorry the Rule is so messy, I'm new to this...and really... I am trying! Anyway, the dilemma comes in when it's the other way around. so instead of the above URL, the user would navigate to something like this... http://www.example.com/movies/page/3/genres/Comedy/12/ any ideas how to introduce some logic? - I've tried grouping the page portion and getting the value from that (may not have been correct) - I also thought if (and only if) a rewrite rule is true, it could go to another rule, sort of like an IF statement Other than that, I have no idea what to do. And I've been googling for hours. Thanks, Ryan Quote Link to comment https://forums.phpfreaks.com/topic/163478-solved-mod_rewrite-logic/ Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 I'm not sure what your asking here, But for both URL's your could create 1 complex regex but 2 would be easier to manage, RewriteRule ^movies/([^/]+)/[^/]+/(\d+)/page/(\d+) /browse.php?type=1&option=$1&option_value=$2&page=$3 [L] RewriteRule ^movies/page/(\d+)/([^/]+)/[^/]+/(\d+) /browse.php?type=1&option=$2&option_value=$3&page=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/163478-solved-mod_rewrite-logic/#findComment-862558 Share on other sites More sharing options...
rlelek Posted June 24, 2009 Author Share Posted June 24, 2009 Thank you MadTechie! I was over thinking this i guess... Anyway, thanks for the help! The solution I used is described below... How can I repay you? Is there a "buy (you) a beer" button I can press? lol Solution: RewriteRule ^movies/genres/[^/]+/(\d+)/page/(\d+) /browse.php?type=1&option=genre&option_value=$1&page=$2 [L] RewriteRule ^movies/page/(\d+)/[^/]+/[^/]+/(\d+) /browse.php?type=1&option=genre&option_value=$2&page=$1 [L] the only variables that were required were the genre id and the page # so I just ignored the rest by removing parentheses (no "capture") After I add a function to make sure that page exists, everything will be set! Quote Link to comment https://forums.phpfreaks.com/topic/163478-solved-mod_rewrite-logic/#findComment-862561 Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 It seams so, No buy a beer feature on this forum, but I maybe add it to a wish list lol, in my RegEx i did capture "genres" as I this maybe useful later! but you know the system better than me Quote Link to comment https://forums.phpfreaks.com/topic/163478-solved-mod_rewrite-logic/#findComment-862569 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.