问题描述
我有一个包含超过 3000 张图片的图片库,并且还在不断增加。我访问图像的网址如下:
1.
http://example.com/gallery/image/cloud-blue-plane-hawaii-428459
or
2.
http://example.com/gallery/image/428459
http://example.com/gallery/image.PHP?image=428459
所以,我需要编写一个 RewriteRule 来访问它。
我相信第二个网址的正确规则(http://example.com/gallery/image/428459
) 如下:
RewriteRule ^gallery/image/(\d+|[^/]+)/?$ gallery/image.PHP?image=$1 [L]
当需要为第一个 url ( http://example.com/gallery/image/cloud-blue-plane-hawaii-428459
) 编写规则时,我有点不知所措……我不知道……
有没有办法为两个 url 制定一个规则?
解决方法
使用
^gallery/image/[^/]*?([0-9]+)/?$
见proof。
说明
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
gallery/image/ 'gallery/image/'
--------------------------------------------------------------------------------
[^/]*? any character except: '/' (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[0-9]+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
/? '/' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n,and the end of the
string