Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule - Please help

         

tc10

9:23 am on May 16, 2012 (gmt 0)

10+ Year Member



I would like to know if it is possible to redirect

from: example.com/archives/80s_files/19841215.html

to: example.com/the-80s-archives/my-box-top-100-121584/

As you can see date formats are different. 1984/12/15 and 12/15/84

Thank You in advance, I've been racking my brains trying to figure this out... yes I am quite new to Mod Rewrites.

tc10

9:48 am on May 16, 2012 (gmt 0)

10+ Year Member



Searching through this forum I am finding a treasure chest of information. I'm going to try to figure this out, but I sure won't turn away any help I can get.

lucy24

11:51 am on May 16, 2012 (gmt 0)

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



Changing the date format is the easiest part. A simple Regular Expression will do it. So give it your best shot and then everyone else can come along and poke holes in it until it rinses clean. Gosh, I hope you're a native speaker of English.

tc10

3:43 pm on May 16, 2012 (gmt 0)

10+ Year Member



Thanks Lucy, I'll give it a shot. I see from my searches here that the date question has been asked like a zillion times. I really should have searched before asking.

tc10

6:16 am on May 17, 2012 (gmt 0)

10+ Year Member



OK, don't beat up on me too bad. I'm a complete rewrite newbie. LOL

Here's what I have so far

RewriteRule ^archives/80s_files/19([0-9]{1,2})([0-9]{1,2})([0-9]{1,2}).html$http://example.com/the-80s-archives/my-box-top-100-$2$3$1/[R=301,NC,L]

Also, how can I make the first digit not show at all if the number is zero? The first ([0-9]{1,2}) I hope that makes sense.

tc10

6:39 am on May 17, 2012 (gmt 0)

10+ Year Member



Actually if the date is like 010184 I need it to be directed to 10184 (without the preceding '0').

tc10

7:44 am on May 17, 2012 (gmt 0)

10+ Year Member



OK, got it all working by creating 2 rules.

RewriteRule ^archives/([0-9])0s_files/19([0-9]{1,2})([1-3])([0-9])([0-9]{1,2}).html$http://example.com/the-$10s-archives/my-box-top-100-$3$4$5$2/[R=301,NC,L]

RewriteRule ^archives/([0-9])0s_files/19([0-9]{1,2})0([0-9])([0-9]{1,2}).html$http://example.com/the-$10s-archives/my-box-top-100-$3$4$2/[R=301,NC,L]

It's probably sloppy, but I'm doing my best figuring this stuff out.

lucy24

11:25 am on May 17, 2012 (gmt 0)

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



Also, how can I make the first digit not show at all if the number is zero? The first ([0-9]{1,2})

Is that the month or the date?

date >> (?:0([1-9])|([123][0-9]))

month >> (?:0([1-9])|(1[012]))

Both are a bit awkward because the single capture has to be treated as a package of two-- or vice versa-- so you have to remember to say $1$2 when you mean $1. The outer parentheses are containers for the pipe.

Don't cut and paste; there's a simpler way I'm overlooking.

The form
([0-9]{1,2})([0-9]{1,2})([0-9]{1,2})

is akin to the dreaded (.+)(.+)(.+) on a smaller scale. Given the choice between 1 and 2, the Regular Expression will always capture 2. It may then have to backtrack if there isn't at least one left over for the later captures.

The year will always have the same number of digits, so that will definitely be 19[0-9][0-9] or simply 19\d\d. (Apache doesn't recognize all abbreviations, but it knows \d and \w.)

19\d\d 0?\d{1,2} [0-3][0-9]

Also not perfect. Does the leading zero have to go away?

g1smd

11:42 am on May 17, 2012 (gmt 0)

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



Given an international readership, you are well advised to use YYYY-MM-DD as the date format (with or without hyphens) with a four digit year and with a leading zero for both month and day. This is easier to parse and it sorts into date order in an alpha sort. See ANSI X3.30, EN 28601, ISO 8601, RFC 3339 etc.

011290 could be Jan 12th or 1st December. 19901201 is always December 1st.

Ha. This post almost repeats what I said in my very first WebmasterWorld post 9 years, 10 months and 2 weeks ago.

An even bigger problem for you is that 11290 could be 1/12/90 (1990-01-12 or 1990-12-01) or 11/2/90 (1990-11-02 or 1990-02-11). By eliminating the 0 you are introducing several layers of complexity into the programming. Use an 8 digit YYYYMMDD number to eliminate all these potential problems.

tc10

8:27 pm on May 17, 2012 (gmt 0)

10+ Year Member



Thanks for the tips, you all rock!

I'm glad that you didn't just give me the code in the beginning because I probably would have just copied and pasted and never learned a thing. At first glance every thing looked a bit confusing but now it's all starting to come together for me. I'm sure that I will have even more questions as I continue to learn the process of mod_rewrites.

~Scott

lucy24

11:38 pm on May 17, 2012 (gmt 0)

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



I'm glad that you didn't just give me the code in the beginning because I probably would have just copied and pasted and never learned a thing.

Psst! Moderator! Can you put this in 96 point blinking* red type at the top of the forum?

:)


* Joke.