问题
Was trying to rewrite the URL of a page that has multiple parameters and one of the parameters contains multiple words (separated by spaces when submitted).
This would be the URL: www.ABCD.com/store/itemsDescr.php?categ=headbands&id=123&name=brown%20with%20black
I would like the URL to show like this: www.ABCD.com/store/headbands/123/brown-with-black
I tried this, but it did not work:
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]
Any help would be much appreciated!
NOTE: Before this rule I want to implement, I already have this other rule for another page, in case it can create conflict:
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:
Enable mod_rewrite and .htaccess through httpd.conf
and then put this code in your .htaccess
under DOCUMENT_ROOT
directory:
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]
来源:https://stackoverflow.com/questions/17326540/rewriting-url-that-contains-multiple-parameters-with-multi-word-strings-with-mo