使用目录的第一个字母的简单正则表达式 .htaccess



我需要一个简单的规则htaccess。我刚刚看到这个页面,但对我不起作用。

例如,我有这个网址:

http://img.myp.local/thumbs/640x700/Birra/nba_jordan_chicago.jpg

我需要解决这个网址:

/cache/Birra/B/nba_jordan_chicago640x700

在哪里可以找到:

 1. /cache/
 2. third directory
 3. first letter of third directory
 4. name of the file (without .jpg)
 5. second directory

这时我写了这个规则:

RewriteEngine on
RewriteRule ^(.*)/([^/]+)/([^/]+).jpg?$ /cache/$2/${lc:$2}/$3$1 [L]

结果是/cache/Birra//nba_jordan_18640x700可以看到缺少 第三个目录的第一个字母

感谢您的帮助,斯特凡诺

编辑 1亚当的解决方案效果很好!但我还有另一个问题。

如果结果/cache/Birra/B/nba_jordan_chicago640x700不存在,我可以得到另一个网址吗? /OTHER_DIRECTORY/640x700/Birra/nba_jordan_chicago.jpg

格拉齐!

编辑 2

伙计们,这是回退的解决方案:

RewriteCond %{DOCUMENT_ROOT}/cache/$2/$3/$4$1 -f
RewriteRule ^(.*)/(([^/])[^/]*)/([^/]+).jpg?$ /cache/$2/$3/$4$1 [L]
RewriteCond %{DOCUMENT_ROOT}/cache/$2/$3/$4$1 !-f
RewriteRule ^(.*)/(([^/])[^/]*)/([^/]+).jpg?$ /thumbs/$1/$2/$4.jpg [L]

再次感谢您的帮助

编辑 3

@Adam:这对我来说非常有用:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{DOCUMENT_ROOT}/cache/$2/$3/$4$1 -f
    RewriteRule ^(.*)/(([^/])[^/]*)/([^/]+).jpg?$ /cache/$2/$3/$4$1 [L]
    RewriteCond %{DOCUMENT_ROOT}/cache/$2/$3/$4$1 !-f
    RewriteRule ^(.*)/(([^/])[^/]*)/([^/]+).jpg?$ /thumbs/$1/$2/$4.jpg [L]
</IfModule>

这将捕获单词的第一个字母,因此可以使用两次:

RewriteEngine on
RewriteRule ^(.*)/(([^/])[^/]*)/([^/]+).jpg?$ /cache/$2/$3/$4$1 [L]

如果要在该文件不存在的情况下进行回退,可以将RewriteCond与-f一起使用:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)/(([^/])[^/]*)/([^/]+).jpg?$
RewriteCond %{DOCUMENT_ROOT}/cache/%2/%3/%4%1 -f
RewriteRule /cache/%2/%3/%4%1 [L]
RewriteRule ^(.*)/(([^/])[^/]*)/([^/]+).jpg?$ /OTHER_DIRECTORY/$2/$3/$4$1 [L]

仅当存在此类文件时,这才会转到/cache位置,否则会转到 /OTHER_DIRECTORY . 我生锈了;我已经很多年没有做过这种事情了,我目前缺少一个Apache httpd实例来试验,但这应该非常接近。

这可能更好,因为它专门忽略第一个路径组件并为文字"添加匹配项"。(而不是任何字符)在末尾:

^                  # beginning of string
/                  # literal '/'
[^/]+?             # one or more chars except '/' - non-greedy
/                  # literal '/'
(                  # start capture group 1
  [^/]+?           # one or more chars except '/' - non-greedy
)                  # end capture group 1
/                  # literal '/'
(                  # start capture group 2
  (                # start capture group 3
  .                # first character
  )                # end capture group 3
  [^/]+?           # one or more chars except '/' - non-greedy
)                  # end capture group 2
/                  # literal '/'
(                  # start capture group 4
  .+?              # one or more chars - non-greedy
)                  # end capture group 4
.                 # literal '.'
jpg                # literal 'jpg'
$                  # end of string

因此:

RewriteRule ^/[^/]+?/([^/]+?)/((.)[^/]+?)/(.+?).jpg$ /cache/$2/$3/$4$1
我想

正则表达式需要一些调整:

RewriteEngine on
RewriteRule ^([^/]*?)/([^/]*)/([^/])([^/]*)/([^/]+).jpg?$ /cache/$3$4/$3/$5$2 [L]

在: http://img.myp.local/thumbs/640x700/Birra/nba_jordan_chicago.jpg

出: http://img.myp.local/cache/Birra/B/nba_jordan_chicago640x700

在 http://htaccess.madewithlove.be 上测试。

最新更新