且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

301 通过 .htaccess 将查询字符串重定向到 SEO 友好的 URL

更新时间:2023-11-07 23:21:04

这应该可以解决问题:

重写引擎开启

## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory/seo/%1/? [L,R=301]

## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory/seo/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]

如果需要,您还可以匹配大小和颜色,将它们也更改为正则表达式组,并使用相应的 %N

You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N

希望这会有所帮助.