.htaccessurlhttp-redirect

How to redirect URL containing question mark and a specific string to the root domain with .htaccess


The old url structure of my website was something like this

https://www.naturablooms.com/wholesale-pricelist?color_id&flower_type_id&name_search_start&page=1&product_type=Dried&psearch&ship_service

Our website current url structure changed for SEO. Now we have to redirect all this kind of url which has color_id after the question mark to root domain. Which is like

from

https://www.naturablooms.com/wholesale-pricelist?color_id&flower_type_id&name_search_start&page=1&product_type=Dried&psearch&ship_service

to

https://www.naturablooms.com/

We tried the following in the .htaccess

RewriteEngine On
RewriteCond %{QUERY_STRING} ^color_id.*$
RewriteRule ^(.*)$ /$1? [R=301,L]

but it will redirect to https://www.naturablooms.com/wholesale-pricelist not the root domain https://www.naturablooms.com/

We need to redirect it to https://www.naturablooms.com/

I will really appreciate if someone help me to figure and resolve this.


Solution

  • This probably is what you are looking for:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^color_id&
    RewriteRule ^ / [R=301,L,QSD]
    

    Or maybe that, this is not really clear from your question:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^color_id&
    RewriteRule ^/?wholesale-pricelist/?$ / [R=301,L,QSD]
    

    Your previous rule explicitly copied the requested path, since you were using the placeholder $1 which contains whatever is captured by the matching pattern. You neither need to capture anything, nor do you need to copy anything.