Search code examples
.htaccessmod-rewriteurl-rewritingamp-html

.htaccess rewriting URL to add a character to a URL


I'm doing this to fix an error with my AMP pages. I'm using the Automatic AMP plugin, and this plugin lets you access the AMP pages using 2 different methods

  1. site.com/post/amp/
  2. site.com/post/?amp

Using the AMP Page validator (https://validator.ampproject.org) I see that all AMP pages using just /amp/ get multiple errors, while /?amp is validated correctly.

Unfortunately, Google is checking /amp/ for all my pages, hence I'm getting tons of errors.

What I'd like to know is how to use the .htaccess redirect rule to add the ? to the AMP queries so all /amp/ requests are redirected (with a 301) to /?amp/

I'd appreciate suggestions on this. Thank you


Solution

  • Something like this:

    RewriteEngine on
    RewriteRule ^(.*)/amp/$ /$1?amp [R=301,L]
    

    This will redirect any URL ending in /amp/ to the same but with ?amp instead, which seems to be what you want. To go in your root .htaccess file.