简体   繁体   中英

htaccess redirect with query string not working

enter code here I have a WordPress website.

I have URLs for affiliates that look like this:

https://example.com/folder/?ref=23432
https://example.com/folder/?ref=13442
etc.

I would like to redirect any URL that ends in ?ref= to another domain.

For example, https://example.com/folder/?ref= should redirect to https://example.org/product/

How can I do this? I appreciate your time.

I tried

Redirect 301 example.com/folder/?ref   https://example.org/product/

Thank you @MrWhite. I tried the following with no success.

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]

The RewriteRule directive matches the URL-path only (less the slash prefix). So this should be matching against folder/ (as per your example), not the hostname.

And the $0 backreference in the substitution string is not required here. So this should simply be:

:
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]

If you do need to check the requested hostname (ie. example.com ) - if example.com and example.org point to the same server - then you need a separate condition ( RewriteCond directive). For example, the complete rule would then become:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]

Note that the regex (^|&)ref= matches the ref= URL parameter anywhere in the query string, if there happened to be other URL parameters that preceded it.

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM