Jump to content

[SOLVED] mod_rewrite logic?


Recommended Posts

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!  :D

 

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

Link to comment
https://forums.phpfreaks.com/topic/163478-solved-mod_rewrite-logic/
Share on other sites

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]

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 :D

 

 

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.