简体   繁体   中英

URL Rewrite for Custom WordPress page with multiple url parameters

My WordPress site has a url that looks like this

https://example.com/coupon/?drug=PHENTERMINE&ndc=10702002501&quantity=15&zipCode=12084&radius=10

but it needs to look like this

https://example.com/coupon/phentermine

I'm not sure how WordPress permalinks play into .htaccess in that environment, but wonder if that's part of my issue?

The URL parameters are necessary to pull the correct data from an API. The site also uses CPT-UI and Advanced Custom Fields but I don't think that matters for this question.

So far, my htaccess code looks like this:

RewriteEngine on 

RewriteRule ^\/coupon\/\?drug=([A-Z]+)\&ndc=[0-9]+\&quantity=[0-9]+\&zipCode=[0-9]&radius=[0-9]$   \/coupon\/$1  [NC,L]

But per https://htaccess.madewithlove.com/ I get "the rule is not met".

Any pointers are much appreciated.

My apologies, i cannot test this simply at this time, but this should work. it is conditional which means you can extend it and add more conditions / other URL parameters. Hope this helps.

You can embed this in your theme functions.php file or any other
PHP file included into your functions.php file.

add_action( 'init', 'tampa_coupon_redirect');
function tampa_coupon_redirect() {
    if (!empty(get_query_var('coupon'))) {
        wp_redirect( site_url('/coupon/').get_query_var('coupon') );
        exit; 
    }   
}

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