Ogugua Belonwu
Ogugua Belonwu

Reputation: 2141

Url Rewrite for year, month and day

I want to achieve something like this: http://example.com/year/month/day instead of http://example.com?year=yyyy&month=mm&day=dd

I want it to rewrite only when the matches are numbers only, when the first segment (year) is a 4 digit no, the 2nd segment month is between 01 and 12 (months of d year), the last segment (day) is between 01 - 31 (days in a month).

Please how can i achieve this

Upvotes: 2

Views: 756

Answers (2)

Timofey Stolbov
Timofey Stolbov

Reputation: 4621

I think you should check month and day number in your script. But here is regexp.

Updated to match single digit day or month with optional trailing slash.

RewriteRule ^(\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|[12][0-9]|3[01])/?$ ?y=$1&m=$2&d=$3

For year and month only with optional trailing slash.

RewriteRule ^(\d{4})/(0?[1-9]|1[0-2])/?$ ?y=$1&m=$2

Upvotes: 3

Teneff
Teneff

Reputation: 32148

the best I can think of is this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d{4})/(0[1-9]|10|11|12)/(0[1-9]|[12][0-9]|3[01])/?$  index.php?y=$1&m=$2&d=$3 [L]

Upvotes: 2

Related Questions