关于带ID的301重定向(htaccess)至新域名的技术问询
key.php with Query Parameters in .htaccess Hey Matthias, let's get that redirect issue sorted out! The rule you're currently using targets the root URL (^$) with a query string starting with id=00, which is why it doesn't work for your key.php links—we need to adjust it to specifically match that file while preserving all your query parameters.
Here's the solution you need:
Add these lines to your .htaccess file on the old domain (old-domain.de):
# Redirect key.php from old-domain.de to newdomain.de, preserving all query params RewriteCond %{HTTP_HOST} ^www\.old-domain\.de$ [NC] RewriteRule ^key\.php$ https://www.newdomain.de/key.php [QSA,R=301,L]
Let's break down each part:
RewriteCond %{HTTP_HOST} ^www\.old-domain\.de$ [NC]: Ensures this rule only runs when the request is to your old domain (theNCflag makes it case-insensitive, so it works if someone types uppercase letters).RewriteRule ^key\.php$ https://www.newdomain.de/key.php: Explicitly matches thekey.phpfile at the root of the old domain, and redirects it to the same file path on the new domain.QSAflag: Short for "Query String Append"—it automatically preserves all existing query parameters (likeid=0000andap=ap_id) without you having to manually write them out.R=301: Tells browsers and search engines this is a permanent redirect (ideal for preserving SEO value).Lflag: Stops processing any further rewrite rules once this one is applied, preventing conflicts with other rules.
If you only want to redirect key.php URLs where the id starts with 00 (matching your existing rule's behavior):
Add an extra condition to narrow down the redirect:
# Redirect key.php with id starting with 00 from old-domain.de to newdomain.de RewriteCond %{HTTP_HOST} ^www\.old-domain\.de$ [NC] RewriteCond %{QUERY_STRING} id=00 [NC] RewriteRule ^key\.php$ https://www.newdomain.de/key.php [QSA,R=301,L]
Quick testing tip:
When you first set this up, use R=302 instead of R=301—temporary redirects don't get cached by browsers, so you can tweak the rule without worrying about stale cache messing up your tests. Once you confirm everything works as expected, switch back to R=301.
内容的提问来源于stack exchange,提问作者Matthias

