Mostafa Norzade
Mostafa Norzade

Reputation: 1766

htaccess redirect all routes start with xxx query string

I use the below code in .htaccess for redirecting the 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!

How can I fix it?

Upvotes: 1

Views: 6880

Answers (1)

Kaddath
Kaddath

Reputation: 6151

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=(.*)

Upvotes: 2

Related Questions