Php 置换与组合

Php 置换与组合,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,有没有办法在htaccess中使用置换来重写规则? 例如,我有一个url: http://localmachine/index.php?c=category&brand=mybrand&country=mycountry&offer=yes&new=yes 重写url应如下所示: http://localmachine/category/brand-mybrand/country-mycountry/offer-yes/new-yes 但我想在url上的

有没有办法在htaccess中使用置换来重写规则?
例如,我有一个url:

http://localmachine/index.php?c=category&brand=mybrand&country=mycountry&offer=yes&new=yes
重写url应如下所示:

   http://localmachine/category/brand-mybrand/country-mycountry/offer-yes/new-yes
但我想在url上的任何位置使用品牌、国家、优惠和新产品。
在url中,我可以在任何位置使用2个、3个或全部

这是我的htaccess:

RewriteRule ^([^/]+)/brand-([^/]+)/?$ index.php?c=$1&brand=$2 [QSA,L]
RewriteRule ^([^/]+)/gramaj-([^/]+)/?$ index.php?c=$1&gramaj=$2 [QSA,L]
RewriteRule ^([^/]+)/oferta-da/?$ index.php?c=$1&oferta=da [QSA,L]
RewriteRule ^([^/]+)/nou-da/?$ index.php?c=$1&nou=da [QSA,L]


RewriteRule ^([^/]+)/brand-([^/]+)/gramaj-([^/]+)/?$ index.php?c=$1&brand=$2&gramaj=$3 [QSA,L]
RewriteRule ^([^/]+)/brand-([^/]+)/nou-da/?$ index.php?c=$1&brand=$2&nou=da [QSA,L]
RewriteRule ^([^/]+)/brand-([^/]+)/oferta-da/?$ index.php?c=$1&brand=$2&oferta=da [QSA,L]
RewriteRule ^([^/]+)/gramaj-([^/]+)/nou-da/?$ index.php?c=$1&gramaj=$2&nou=da [QSA,L]
RewriteRule ^([^/]+)/gramaj-([^/]+)/oferta-da/?$ index.php?c=$1&gramaj=$2&oferta=da [QSA,L]
RewriteRule ^([^/]+)/gramaj-([^/]+)/brand-([^/]+)/?$ index.php?c=$1&gramaj=$2&brand=$3 [QSA,L]
RewriteRule ^([^/]+)/oferta-da/nou-da/?$ index.php?c=$1&oferta=da&nou=da [QSA,L]
RewriteRule ^([^/]+)/oferta-da/gramaj-([^/]+)/?$ index.php?c=$1&oferta=da&gramaj=$2 [QSA,L]
RewriteRule ^([^/]+)/oferta-da/brand-([^/]+)/?$ index.php?c=$1&oferta=da&brand=$2 [QSA,L]
RewriteRule ^([^/]+)/nou-da/gramaj-([^/]+)/?$ index.php?c=$1&nou=da&gramaj=$2 [QSA,L]
RewriteRule ^([^/]+)/nou-da/brand-([^/]+)/?$ index.php?c=$1&nou=da&brand=$2 [QSA,L]
RewriteRule ^([^/]+)/nou-da/oferta-da/?$ index.php?c=$1&nou=da&oferta=da [QSA,L]

此基于递归的规则适用于您:

RewriteEngine On

# rewrite /category to /index.php?c=category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php/?c=$1 [L,QSA]

# rewrite /category/brand-mybrand/country-mycountry/offer-yes/new-yes
# to /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^-]+.+)/?$ /index.php/$2?c=$1 [L,QSA]

# rewrite /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
# converts any /name-val/ to query parameter name=val in every rewrite
# stopping when there is no part left after /index.php
RewriteRule ^(index\.php)/([^-]+)-([^/]+)(/.*)?$ /$1$4?$2=$3 [L,QSA]
这将重写:

/category/brand-mybrand/country-mycountry/offer-yes/new-yes
为此:

/index.php?new=yes&offer=yes&country=mycountry&brand=mybrand&c=category

PS:我强烈建议您先独立测试此代码,然后在这两条规则下添加规则

是否总是在开始时进行
/category/
分类总是在开始时进行?分类总是在开始时进行过滤系统提示:不要乱动
.htaccess
。将
*
重定向到
index.php
并用php进行解析,这会更方便。您能解释一下吗?这里有双引号的是-否问题。由于规则模式中有空格,如果您不想删除它们,可以将其删除。您能解释一下吗?每个规则我都可以在答案中添加解释,但它对你有用吗?是的,但不适用于作为起点的类别。例如:正在使用添加的解释,并且
www.localhost/category/brand mybrand
对我有用。