使用多字符串重写包含多个参数的URL(使用mod_rewrite)

时间:2013-06-26 17:19:19

标签: .htaccess url redirect mod-rewrite rewrite

尝试重写具有多个参数的页面的URL,其中一个参数包含多个单词(提交时用空格分隔)。

这就是网址: www.ABCD.com/store/itemsDescr.php?categ=headbands&id=123&name=brown%20with%20black

我希望网址显示如下: www.ABCD.com/store/headbands/123/brown-with-black

我尝试了这个,但它不起作用:

RewriteCond %{QUERY_STRING} ^categ=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &categ=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php$ $0/%1

RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &id=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php/[^/]+$ $0/%1

RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &name=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php/([^/]+/[^/]+)$ http://www.ABCD.com/store/$1/%1/? [L,QSA,NC]

非常感谢任何帮助!

注意:在我要实施此规则之前,我已经有另一个规则用于另一个页面,以防它可能产生冲突:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(store)/index\.php\?categ=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule ^store/(.+?)/?$ /store/index.php?categ=$1 [L,QSA,NC]

1 个答案:

答案 0 :(得分:1)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^categ=([^&]+)&id=([^&]+)&name=([^&]+)$ [NC]
RewriteRule ^(store)/itemsDescr\.php$ /$1/%1/%2/%3? [R=302,L,NC,NE]

RewriteRule ^(store)/([^\s]+)\s([^\s]+)$ /$1/$2-$3 [R=302,L,NC]

RewriteRule ^(store)/([^\s]+)\s(.+)$ /$1/$2-$3 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(store)/([^/]+)/([^/]+)/ /$1/itemsDescr.php?categ=$2&id=$3 [L,QSA,NC,NE]