2

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.

4
  • What are the full "variation" URLs that are causing issues? I would not expect the URLs you've posted (with just an extra URL param) to result in a 404. So what URLs are resulting in "thousand of not found"? What is the corresponding "permalink" structure of these URLs?
    – MrWhite
    Commented Feb 25, 2022 at 9:31
  • 1
    i was afraid of mixing questions..but there's URL that I didn't include since Im not sure if it's parameter or just path /amp/ /amp..you're correct ?amp= and ?param did not cause 404 error but /amp/ does..there're created by amp plugin..my apologize if I'm confused you
    – R freel
    Commented Feb 25, 2022 at 9:40
  • You should add that to your question and include the full URL format (and corresponding permalink URL format). Whilst that is a different URL and requires a different regex/condition, it is part of the same problem and could probably be resolved with a single rule. To clarify, is the .html extension present on all these URLs and part of your permalink structure?
    – MrWhite
    Commented Feb 25, 2022 at 9:59
  • MrWhite , I've edited my original question
    – R freel
    Commented Feb 25, 2022 at 10:10

1 Answer 1

2

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:

3
  • thank you MrWhite..i add the rules on my vhost.conf but it didn't work but its working on my htaccess
    – R freel
    Commented Feb 25, 2022 at 11:06
  • @Rfreel If the rule is added directly in the <VirtualHost> config then you need to allow for a slash at the start of the URL-path. I've updated my answer to allow for this. So that it would work in both the vHost and .htaccess. (mod_rewrite behaves slightly differently when used in a server context as opposed to a directory context.)
    – MrWhite
    Commented Feb 25, 2022 at 11:13
  • clear as Crystal ..Thank you very much
    – R freel
    Commented Feb 25, 2022 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.