.htaccess 重写规则更改下划线为破折号


.htaccess RewriteRule change underscore to dash

我现在在我的.htaccess文件中有一个重写规则:

RewriteRule ^item/?([a-zA-Z0-9_]+)?/?([a-zA-Z0-9_]+)?/?$ static/items.php?a=$1&b=$2 [NC,L]

它将选取任何带有下划线的项目:

item/new_item/new_order

但是,我需要从下划线更改为破折号才能实现:

item/new-item/new-order

如果我只是在重写规则字符串中进行更改,它会破坏它。不知道如何解决这个问题。

RewriteRule ^item/?([a-zA-Z0-9-]+)?/?([a-zA-Z0-9-]+)?/?$ static/items.php?a=$1&b=$2 [NC,L]

这是相当棘手的事情,但可以使用以下Rewrite规则来完成:

RewriteEngine On
# first replace each _ by - recursively
RewriteRule ^(item)/([^_]*)_(.*)$ /$1/$2-$3 [L,NC]
# now usual stuff to forward request to static/item.php
RewriteRule ^(item)/([^_/]*)/?([^_/]*)/?$ static/$1.php?a=$2&b=$3 [L,QSA,NC]