.htaccess RewriteRule更改下划线变为破折号 [英] .htaccess RewriteRule change underscore to dash

查看:65
本文介绍了.htaccess RewriteRule更改下划线变为破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a ReWrite rule in my .htaccess file right now:

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

它将选择所有带有下划线的项目:

It will pick any items that have an underscore:

item/new_item/new_order

但是,我需要将其从下划线更改为破折号:

However, I need to change from underscore to dashes to make it:

item/new-item/new-order

如果我只是在RewriteRule字符串中进行更改,它将破坏它.不知道如何解决这个问题.

If I simply make a change in RewriteRule string it breaks it. Not sure how to fix that.

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

推荐答案

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

This is fairly tricky stuff but can be done using following Rewrite rules:

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]

这篇关于.htaccess RewriteRule更改下划线变为破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆