3

I'm trying to create a 301-rewrite for URLs ending with ?amp=1 to nearly the same URL and query string, just stripping the value (=1).

Examples:

  • https://example.com/?amp=1 should redirect to https://example.com/?amp
  • https://example.com/page/?amp=1 should redirect to https://example.com/page/?amp

I've managed to get the RewriteCond right to match only URLs ending with ?amp=1, i.e.:

RewriteCond %{QUERY_STRING} ^/?amp=1$

but can't get the RewriteRule correct to go to the URL without the =1 value.

4
  • 2
    You should also provide your RewriteRule in your question, even if it fails.
    – Éric
    Commented Mar 29, 2021 at 6:50
  • Should we consider that there might be other variables in the query string (e.g., ?amp=1&foo=qux&bar=quux)?
    – Éric
    Commented Mar 29, 2021 at 7:44
  • In what context are you using these directives? Main server config/virtualhost or directory (.htaccess)?
    – MrWhite
    Commented Mar 29, 2021 at 23:46
  • "You should also provide your RewriteRule in your question, even if it fails." What I have been able to produce, and test with online tools (not live server) is in the post. "Should we consider that there might be other variables in the query string (e.g., ?amp=1&foo=qux&bar=quux)?" Never any other variables. Only looking to address urls ending in ?amp=1 specifically. "In what context are you using these directives? Main server config/virtualhost or directory (.htaccess)?" In .htaccess Thanks all.
    – andrewgoma
    Commented Mar 30, 2021 at 3:57

1 Answer 1

2

Your RewriteCond is incidentally valid but fundamentally wrong. For the URL https://example.com/?amp=1, the characters /? are not part of the %{QUERY_STRING} value, which is amp=1 in fact.
I said 'incidentally valid' because in your regex, / is neutralized by ?, so there is a match with amp=1.

To achieve your goal, in the simplest case, especially without the possibility of other variables in the query string, an Apache .htaccess file could include these directives:

# Tested with Apache httpd 2.4 on Debian 10
RewriteCond %{QUERY_STRING} ^amp=1$
RewriteRule (.*) /$1?amp [NE,R=301,L]

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.