用if和else

时间:2015-07-31 13:51:52

标签: .htaccess mod-rewrite if-statement url-rewriting

我必须为我的重写规则添加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]

有可能吗?提前致谢

1 个答案:

答案 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]