大网数据广告活动
网站运营与故障问答

Handle "amp" in URLs from a now removed WordPress plugin

· 52CE 我爱测

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.exam…

问题原文

原帖链接 · 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:

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2022-02-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。