我必须为我的重写规则添加if和else。
我的重写就是这个
RewriteCond %{THE_REQUEST} \ /+catalogsearch/result/\?cat=(.*)&q=([^&\ ]+)
RewriteRule ^ /q_%2_cat_%1? [L,NE,R=301]
RewriteRule ^q_(.*)_cat_(.*)?$ /index.php/catalogsearch/result/?q=$1&cat=$2 [QSA,P,NC]
如果cat为null,我想更改为
RewriteCond %{THE_REQUEST} \ /+catalogsearch/result/\?cat=&q=([^&\ ]+)
RewriteRule ^ /q_%1? [L,NE,R=301]
RewriteRule ^q_(.*)$ /index.php/catalogsearch/result/?q=$1 [QSA,P,NC]
有可能吗?提前致谢
答案 0 :(得分:1)
只需将这两个规则一个接一个地放在一起,而不是使用.*
,请使用.+
。此外,您不需要P
标志,而是使用L
标志结束重写:
RewriteCond %{THE_REQUEST} \ /+catalogsearch/result/\?cat=(.+)&q=([^&\ ]+)
RewriteRule ^ /q_%2_cat_%1? [L,NE,R=301]
RewriteRule ^q_(.+)_cat_(.+)$ /index.php/catalogsearch/result/?q=$1&cat=$2 [QSA,L,NC]
RewriteCond %{THE_REQUEST} \ /+catalogsearch/result/\?cat=&q=([^&\ ]+)
RewriteRule ^ /q_%1? [L,NE,R=301]
RewriteRule ^q_([^_]+)$ /index.php/catalogsearch/result/?q=$1 [QSA,L,NC]