Regex 重写规则以提取和重写日期

Regex 重写规则以提取和重写日期,regex,date,mod-rewrite,Regex,Date,Mod Rewrite,有人能帮助我理解如何在.htaccess文件中编写重写规则,以采用dmmmyyyy字符串格式并将其转发到yyyy-mm-dd.html 例如,mydomain.com/7nov2014应该重定向到mydomain.com/path/to/content/2014-11-07.html 此外,如果一天是两位数,它应该仍然有效,例如,mydomain.com/17nov2014应该转到mydoamin.com/path/to/content/2014-11-17.html 所以,如果通过的日期是1位

有人能帮助我理解如何在
.htaccess
文件中编写重写规则,以采用
dmmmyyyy
字符串格式并将其转发到
yyyy-mm-dd.html

例如,
mydomain.com/7nov2014
应该重定向到
mydomain.com/path/to/content/2014-11-07.html

此外,如果一天是两位数,它应该仍然有效,例如,
mydomain.com/17nov2014
应该转到
mydoamin.com/path/to/content/2014-11-17.html

所以,如果通过的日期是1位,则附加0,否则按原样使用


谢谢你的帮助

一个简单的方法是为转换设置一个表

在这里,我们首先检查每月的任何两位数日期
([0-9]{2})
。如果失败了,那么我们看一看单位数的
([0-9]{1})
,这样可以节省时间和开销,因为一个月内两位数的天数多于一位数的天数。这允许我们在单日数字前面附加一个
0
0字符。我们通过括号将其存储在group 1变量中

接下来,我们将3位数的月份代码转换为月份编号;既然我们不打算保留原版,我们就做一个干净的替换品

最后,我们在组
2([0-9]{4})
中捕获4位年份

后面可能跟一个斜杠字符
/?
,因为原始命名约定看起来像一个文件夹

不幸的是,
.htaccess
文件与编程语言并不完全相同,但实际上更多的是一个配置文件,因此没有高级语言中的日期识别和转换功能:

RewriteEngine On
RewriteBase /

RewriteRule ^([0-9]{2})jan([0-9]{4})/?$ path/to/content/$2-01-$1
RewriteRule ^([0-9]{2})feb([0-9]{4})/?$ path/to/content/$2-02-$1
RewriteRule ^([0-9]{2})mar([0-9]{4})/?$ path/to/content/$2-03-$1
RewriteRule ^([0-9]{2})apr([0-9]{4})/?$ path/to/content/$2-04-$1
RewriteRule ^([0-9]{2})may([0-9]{4})/?$ path/to/content/$2-05-$1
RewriteRule ^([0-9]{2})jun([0-9]{4})/?$ path/to/content/$2-06-$1
RewriteRule ^([0-9]{2})jul([0-9]{4})/?$ path/to/content/$2-07-$1
RewriteRule ^([0-9]{2})aug([0-9]{4})/?$ path/to/content/$2-08-$1
RewriteRule ^([0-9]{2})sep([0-9]{4})/?$ path/to/content/$2-09-$1
RewriteRule ^([0-9]{2})oct([0-9]{4})/?$ path/to/content/$2-10-$1
RewriteRule ^([0-9]{2})nov([0-9]{4})/?$ path/to/content/$2-11-$1
RewriteRule ^([0-9]{2})dec([0-9]{4})/?$ path/to/content/$2-12-$1

RewriteRule ^([0-9]{1})jan([0-9]{4})/?$ path/to/content/$2-01-0$1
RewriteRule ^([0-9]{1})feb([0-9]{4})/?$ path/to/content/$2-02-0$1
RewriteRule ^([0-9]{1})mar([0-9]{4})/?$ path/to/content/$2-03-0$1
RewriteRule ^([0-9]{1})apr([0-9]{4})/?$ path/to/content/$2-04-0$1
RewriteRule ^([0-9]{1})may([0-9]{4})/?$ path/to/content/$2-05-0$1
RewriteRule ^([0-9]{1})jun([0-9]{4})/?$ path/to/content/$2-06-0$1
RewriteRule ^([0-9]{1})jul([0-9]{4})/?$ path/to/content/$2-07-0$1
RewriteRule ^([0-9]{1})aug([0-9]{4})/?$ path/to/content/$2-08-0$1
RewriteRule ^([0-9]{1})sep([0-9]{4})/?$ path/to/content/$2-09-0$1
RewriteRule ^([0-9]{1})oct([0-9]{4})/?$ path/to/content/$2-10-0$1
RewriteRule ^([0-9]{1})nov([0-9]{4})/?$ path/to/content/$2-11-0$1
RewriteRule ^([0-9]{1})dec([0-9]{4})/?$ path/to/content/$2-12-0$1
此外,根据,您应该能够充分利用RewriteBase来减少文件中的所有重复路径:

RewriteEngine On
RewriteBase /path/to/content/

RewriteRule ^([0-9]{2})jan([0-9]{4})/?$ $2-01-$1
RewriteRule ^([0-9]{2})feb([0-9]{4})/?$ $2-02-$1
RewriteRule ^([0-9]{2})mar([0-9]{4})/?$ $2-03-$1
RewriteRule ^([0-9]{2})apr([0-9]{4})/?$ $2-04-$1
RewriteRule ^([0-9]{2})may([0-9]{4})/?$ $2-05-$1
RewriteRule ^([0-9]{2})jun([0-9]{4})/?$ $2-06-$1
RewriteRule ^([0-9]{2})jul([0-9]{4})/?$ $2-07-$1
RewriteRule ^([0-9]{2})aug([0-9]{4})/?$ $2-08-$1
RewriteRule ^([0-9]{2})sep([0-9]{4})/?$ $2-09-$1
RewriteRule ^([0-9]{2})oct([0-9]{4})/?$ $2-10-$1
RewriteRule ^([0-9]{2})nov([0-9]{4})/?$ $2-11-$1
RewriteRule ^([0-9]{2})dec([0-9]{4})/?$ $2-12-$1

RewriteRule ^([0-9]{1})jan([0-9]{4})/?$ $2-01-0$1
RewriteRule ^([0-9]{1})feb([0-9]{4})/?$ $2-02-0$1
RewriteRule ^([0-9]{1})mar([0-9]{4})/?$ $2-03-0$1
RewriteRule ^([0-9]{1})apr([0-9]{4})/?$ $2-04-0$1
RewriteRule ^([0-9]{1})may([0-9]{4})/?$ $2-05-0$1
RewriteRule ^([0-9]{1})jun([0-9]{4})/?$ $2-06-0$1
RewriteRule ^([0-9]{1})jul([0-9]{4})/?$ $2-07-0$1
RewriteRule ^([0-9]{1})aug([0-9]{4})/?$ $2-08-0$1
RewriteRule ^([0-9]{1})sep([0-9]{4})/?$ $2-09-0$1
RewriteRule ^([0-9]{1})oct([0-9]{4})/?$ $2-10-0$1
RewriteRule ^([0-9]{1})nov([0-9]{4})/?$ $2-11-0$1
RewriteRule ^([0-9]{1})dec([0-9]{4})/?$ $2-12-0$1

您可以先查找个位数,但系统会对每个可能的转换进行所有个位数的检查。您可以通过环顾四周和查找表使转换更加复杂,但这是最简单的直接方法,一目了然。

谢谢@AbsoluteƵERØ!