修复 RewriteRule - 访问由连字符分隔的字符串末尾的 ID

Fix a RewriteRule - Access an ID at the end of a string separated by a Hyphen

我有一个图片库,里面有超过 3000 张图片,而且还在不断增加。我的 url 访问图像如下:

1.
http://example.com/gallery/image/cloud-blue-plane-hawaii-428459

or 

2.
http://example.com/gallery/image/428459

PHP 脚本可在此处找到:

http://example.com/gallery/image.php?image=428459

所以,我需要编写一个 RewriteRule 来访问它。

我认为第二个规则正确url(http://example.com/gallery/image/428459 ) 如下:

RewriteRule ^gallery/image/(\d+|[^/]+)/?$ gallery/image.php?image= [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 :
--------------------------------------------------------------------------------
    [0-9]+                   digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of 
--------------------------------------------------------------------------------
  /?                       '/' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string