htaccess redirect all routes start with xxx

I use below code in .htaccess for redirect subdomain of Domain1(wordpress) to Domain2 :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^learn.Domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.learn.Domain1\.com$
RewriteRule ^(.*)$ "https\:\/\/\Domain2\.com\/$1" [R=301,L]

all is ok.

but my product downloads link that email to user for download started with this in woocommerce :

learn.Domain1.com/?download_file=xxxxxxxxxxxxxxxxxx

I use this cond redirect except for downloads URL :

RewriteCond %{REQUEST_URI} !^/?download_file=

but this does not work!!

Thanks for help

1 answer

  • answered 2019-06-25 08:41 Kaddath

    You have 2 problems here:

    • the ^ in the regex means "starts by" and your URI doesn't start by the query parameters
    • as stated in the docs, REQUEST_URI doesn't include request parameters, try with QUERY_STRING:
    RewriteCond %{QUERY_STRING} !^download_file=(.*)
    

    NOTE: because of the ^, this will work only if the parameter download_file is the first, if it's not the case, you can use :

    RewriteCond %{QUERY_STRING} !(?:^|&)download_file=(.*)