如何.htaccess重定向到www而不会失去GET

need some help. i have:

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^catalog/ catalog.php
RewriteRule ^catalog/(.*)/ catalog.php?id=$1

with this redirect i don't receive (http://www.example.com/catalog/abc/)

GET['id']

how to receive GET['id']?

Your 2nd rule needs 2 flags:

  • QSA - Query String Append
  • L - Last

Overall your .htaccess should look like this:

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

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^catalog/([^/]+)/?$ catalog.php?id=$1 [L,QSA,NC]

QSA flag in 2nd rule will make sure to presserve any existing query string while adding a new query parameter id