Joe Patterson
Joe Patterson

Reputation: 17

mod_rewrite rule using date regex

I'm trying to write a rule that when user types in this url:

domain.com/09/13/2013/thisIsMyPageTitle

That url stays in browser window, but content from this url is displayed:

domain.com/contentlibrary/thisIsMyPageTitle

This is my rule that I currently get an error with:

RewriteEngine On
RewriteRule ^((0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d[/])$(.*) /contentlibrary/$1  [L]

I'm trying to match the date with regular expression, and use the (.*) from the initial url in the second one that holds the content and actually exists.

Upvotes: 0

Views: 720

Answers (2)

Jon Lin
Jon Lin

Reputation: 143856

The error that you're getting is probably because you have unescaped spaces in your regex. Specifically these:

[- /.]

The spaces get interpreted by mod_rewrite as the delimiter between parameters. Additionally, you have this:

$(.*)

at the end of your pattern. The $ matches the end of the string, so you want those swapped:

(.*)$

So:

^((0[1-9]|1[012])[-\ /.](0[1-9]|[12][0-9]|3[01])[-\ /.](19|20)\d\d[/])(.*)$

shold be the pattern that you want.

Upvotes: 0

anubhava
anubhava

Reputation: 784898

If you're not going to do anything with date then why bother being precise with date semantics. You can simplify your regex:

RewriteRule ^[0-9]+/[0-9]+/[0-9]+/([^/]+)/?$ /contentlibrary/$1 [L]

Upvotes: 3

Related Questions