'301 Redirect to remove query string on homepage only
I don't have knowledge of .htaccess
redirect rules. If a user enters http://example.com?abc=123
or https://example.com?who=123
or https://example.com?xyz=123
then it should redirect to https://example.com
with a 301 redirect.
If a user enters any query string then it should redirect to the homepage, but I need to implement this only on the homepage not inner, plp or pdp page.
My plp URL is just like https://example.com/sale.html
and user can add query string on this page via layered navigation filters. We are ok with plp page.
Please help me and provide me .htaccess
rule or any other way to accomplish this. I am using Magento2 and using Fastly, if anything possible in Magento2 via Fastly then it will easy to implement.
Solution 1:[1]
To remove the query string on any request to the root URL only then add the following at the top of your .htaccess
file:
# Remove query string on homepage only
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ / [QSD,R=301,L]
You do not need to repeat the RewriteEngine On
directive if this already occurs later in the file (which I expect it does).
The RewriteRule
pattern ^$
matches the root only (ie. https://example.com/
). The preceding condition (RewriteCond
directive) checks for the presence of a query string (ie. at least 1 character, denoted by the regex .
).
If this matches then a 301 (permanent) redirect is triggered back to the root (/
), but with the query string removed. The QSD
flag (Query String Discard) removes the original query string from the redirected response.
NB: Test first with a 302 (temporary) redirect (ie. change R=301
to R=302
in the above RewriteRule
directive) to avoid potential caching issues. (301s are cached persistently by the browser so can make testing problematic.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |