问题原文
原帖链接 · R freel · CC BY-SA 4.0
i used an amp plugin earlier but then I disabled it. Now variations of leftover amp path leave me with a thousand of "not found" or extra additional URLs with amp parameters.
https://www.example.com/post.html?amp
https://www.example.com/post.html?amp=0
https://www.example.com/post.html?amp=1
https://www.example.com/post.html?noamp
https://www.example.com/post.html?noamp=0
https://www.example.com/post.html?noamp=1
https://www.example.com/post.html/amp/
https://www.example.com/post.html/amp
Post permalinks is set as /%postname%.html ,so post URL is something like this
https://www.example.com/post.html
htaccess is using auto-generated one by wordpress
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any suggestion on how to handle these URL variations? I'm unsure what to do. I'm thinking to redirect 301 to its permalinks (maybe using htaccess or plugin redirection such as rankmath.) But I'm clueless on how to used regex, maybe someone can share piece of htaccess rule or Rankmath regex rules.
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
Yes, you need to 301 redirect these "amp" URLs back to the permalink in order to preserve SEO and please users.
You can add the following before the existing WP code block, ie. before the # BEGIN WordPress comment marker.
# Redirect old "amp" URLs back to permalink
RewriteCond %{QUERY_STRING} ^(no)?amp(=[01])?$ [OR]
RewriteCond %{REQUEST_URI} /amp/?$
RewriteRule ^/?([^/]+\.html) /$1 [QSD,R=301,L]
The above states... for any URL that starts /<postname>.html that either contains an amp query string as described or the URL-path ends with /amp/ (or /amp) then redirect to /<postname>.html only, stripping any query string that might have been present.
The QSD (Query String Discard) flag removes the query string (if any) from the redirect response.
This does assume that when amp or noamp appears in the query string then this is the only URL parameter.
You do not need to repeat the RewriteEngine directive that already occurs later in the WP code block.
Test first with 302 (temporary) redirects in order to avoid potential caching issues.
See my answer to the following related question for a more general solution:
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
