如何使用.htaccess去除URL末尾多余参数实现重定向
Got it, let's sort out this redirect problem you're facing. The basic Redirect directive isn't cutting it here because it can't handle the specific quirk in your broken URL— that extra dot before the query string, plus the acm parameter tacked on. We'll use Apache's mod_rewrite instead, which lets us use regex to target exactly this issue.
Step 1: Ensure mod_rewrite is enabled
Most Apache hosts have this turned on by default, but if you're unsure, check with your hosting provider or confirm that mod_rewrite is uncommented in your server's httpd.conf file.
Step 2: Add the rewrite rule to your .htaccess
Open (or create) your site's root .htaccess file and add these lines:
# Enable mod_rewrite engine RewriteEngine On # Redirect the broken URL to the correct one, stripping the extra dot and query parameter RewriteCond %{REQUEST_URI} ^/walks-events/huguenot-footsteps/french-protestant-church-library.html\.$ RewriteCond %{QUERY_STRING} ^acm=3466_63$ RewriteRule ^(.*)$ /walks-events/huguenot-footsteps/french-protestant-church-library.html? [R=301,L]
What each part does:
RewriteEngine On: Turns on the mod_rewrite functionality.- The first
RewriteCond: Matches the exact broken path (with the extra dot at the end of.html). - The second
RewriteCond: Targets the specificacm=3466_63query string. RewriteRule: Redirects any matching request to the correct URL. The?at the end ensures the query string is stripped entirely.R=301: Tells browsers and search engines this is a permanent redirect (good for SEO).L: Stops processing further rewrite rules once this one runs.
Need a more generic rule?
If you might have other similar URLs with extra dots and acm parameters, you can use a broader rule to catch all of them:
RewriteEngine On # Strip extra dot after .html and any query parameters RewriteRule ^(.*)\.html\.(.*)$ /$1.html? [R=301,L]
This will match any URL ending with .html. (plus any characters/query params) and redirect to the same path without the extra dot, clearing the query string.
Test it out
After saving the .htaccess file, visit your broken URL (/walks-events/huguenot-footsteps/french-protestant-church-library.html.?acm=3466_63)— you should be automatically sent to the correct, clean version.
内容的提问来源于stack exchange,提问作者philip

