Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite querystring conversion help.

mod_rewrite querystring conversion help..

         

tcampos

8:43 pm on Jan 21, 2010 (gmt 0)

10+ Year Member



hey .. new to this thing and stuck badly on the mod_rewrite code..
can ya help?

I administer example.com and just converted the photo gallery from ugly popups to querystring based embeded awesomeness...

I found out that google hates these kind of urls:
http://www.example.com/light_brown/fuzzy_widgets/?ref=MMP2021&fil=TheFoo_ThursdayNightLounge_1142010_003.jpg&max=37

and could possibly love this URL:
http://www.example.com/light_brown/fuzzy_widgets/gallery/MMP2021/TheFoo/ThursdayNightLounge/1-14-2010/003.jpg
or
http://www.example.com/light_brown/fuzzy_widgets/gallery/MMP2021/TheFoo/ThursdayNightLounge/1-14-2010/003/

I managed to make this work:
http://www.example.com/light_brown/fuzzy_widgets/gallery/MMP2021/
with this code: (in the root of fuzzy_widgets)
RewriteEngine on
RewriteRule ^gallery/([A-Z,0-9]+)/?$ index.php?ref=$1&fil=$2

but the rest just baffles me.

can any one help?

thanks for your help in advance...

[edited by: jdMorgan at 9:24 pm (utc) on Jan. 21, 2010]
[edit reason] No URLs or specific keywords, please. See Terms of Service. [/edit]

jdMorgan

9:27 pm on Jan 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> but the rest just baffles me.

Please be more specific. What else do you want to do that this rule does not do? Or is it something else?

You will need to 'expand' this rule to allow for the additional slash(es) in the SEO-friendly URLs, and you will need to include the "max" number in your SEO-friendly URLs.

Jim

tcampos

10:04 pm on Jan 21, 2010 (gmt 0)

10+ Year Member



Hey Jim...

I want to convert this made up directory structure: /fuzzy_widgets/gallery/MMP2021/TheFoo/ThursdayNightLounge/1-14-2010/003/
to this page as a querystring: /fuzzy_widgets/?ref=MMP2021&fil=TheFoo_ThursdayNightLounge_1142010_003.jpg

I would like to take each directory and build a 2nd querystring value (fil) and put them together via an underscore (_) and remove the dashes between the dates.

I figured out the 1st querystring, but adding a 2nd querysting that has to be build based on multiple directory structure values has got me a bit cookoo...

any help would be awesome!

g1smd

10:28 pm on Jan 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Using underscores in URLs is rarely recommended. Stick with hyphens as separators in URLs if you can. Using underscores internally within the rewrite is much less of a problem.

As for dates, investigate the BigEndian format specified by RFC 3339, where today is 20100121 or 2010-01-21. With that format, files sort into date order when using an alphabetical filename sort, as well as being universally understood (unlike 2-1-2010 which means different things either side of the Atlantic).

jdMorgan

11:11 pm on Jan 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So take each slash- and dash-separated 'field' of the client-requested friendly URL and match it against an appropriate pattern (match upper/lowercase letters, numbers, etc.). You can make the pattern 'forgiving', as in matching 01322010 (January 32nd), or you can make it tighter, as below. You can disallow or require leading zeroes, or make them optional as below. You need a very good 'definition' of what you expect and will allow in each field, and then code each of your subpatterns to match that definition.

See the regular-expressions tutorial cited in our Apache Forum Charter (link at top of this page) for important details.

Once all of the 'fields' are matched by parenthesized subpatterns, they can be back-referenced in the substitution URL by referring to them by number $1 through $9. See the mod_rewrite documentation at apache.org, especially the 'back-references' section and the description of RewriteCond and RewriteRule.

Something like this should get you started:


RewriteRule ^gallery/([A-Z0-9]+)/(A-Za-z)+/([A-Za-z]+)/([0-9]¦[1][012])-([012]?[0-9]¦3[01])-(2[0-9]{3})/[0-9]+$ index.php?ref=$1&fil=$2_$3_$4$5$6_$7 [L]

Jim