简体   繁体   中英

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.

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM