且构网

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

SEO 友好 URL 而不是查询字符串 .htaccess

更新时间:2023-02-23 09:20:34

您需要一组新的 2 个参数规则:

You need new set of rules for 2 parameters:

RewriteEngine on
RewriteBase /example/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:.php)??id=([^s&]+)&color=([^s&]+) [NC]
RewriteRule ^ products/%1/%2/? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /products(?:.php)??id=([^s&]+)s [NC]
RewriteRule ^ products/%1/? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal forward from pretty URL to actual one
RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [NC,L,QSA]

RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [NC,L,QSA]

RewriteRule ^products/?$ products-list.php [L,NC]