这是我的htaccess代码:
RewriteEngine On RewriteRule ^s/(.*)/(.*) /index.php?search=$1&category=$2 [L,QSA] RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$ RewriteRule ^(.*)$ /s/%1/? [R=301,L] RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$ RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
我需要制作一个像这样的重写规则:
http://mywebsite.com/s/query+term => http://mywebsite.com/?search=query+term OR if there is a category http://mywebsite.com/s/query+term/Category => http://mywebsite.com/?search=query+term&category=Category
我还需要将所有旧网址重定向到新网址。
根据这个规则,我可以获得的所有内容都是将搜索词和类别名称连接到togheter。简而言之,就好像我一直如此:
http://mywebsite.com/?search=query+term/Category
如果我删除所有条件(RewriteCond),只留下重写规则,并添加一个尾部斜杠:
RewriteEngine On RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
直接到达我感兴趣的网址,整个过程都有效。但我不会重定向..
所以我试图设置这样的规则:
RewriteEngine On RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA] RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$ RewriteRule ^(.*)$ /s/%1/? [R=301,L] RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$ RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
但是根据这个规则,我只得到一个重定向循环。
答案 0 :(得分:1)
您可以使用以下规则:
RewriteEngine on
#1--Redirect from "?search=foobar&cat=cat" to "/s/foobar/cat" --#
RewriteCond %{THE_REQUEST} /\?search=([^&]+)&cat=([^\s]+) [NC]
RewriteRule ^ /s/%1/%/%2? [NC,L,R]
#2--Redirect from "/?search=foobar" to "/s/foobar" --#
RewriteCond %{THE_REQUEST} /\?search=([^/\s]+) [NC]
RewriteRule ^ /s/%1? [NC,L,R]
#1--Rewrite "s/foobar/cat" to "/?search=foobar&cat=cat"--#
RewriteRule ^s/([^/]+)/([^/]+)/?$ /?search=$1&$category=$2 [NC,L]
#2--Rewrite "s/foobar" to "/?search=foobar"--#
RewriteRule ^s/([^/]+)/?$ /?search=$1 [NC,L]