Home > other >  How to redirect by changing part of a dynamic url
How to redirect by changing part of a dynamic url

Time:06-30

So here is the problem. I am using Elementor Pro for woocomerce and the plugin redirecting the order completion to a wrong url. There is no way at the moment to fix this.

Now the only way to fix it is to change the ?elementor_library=single-product to checkout from the below link.

I want to redirect

https://domain.co.uk/?elementor_library=single-product/order-received/2786/&key=wc_order_i6GIKeD0SaNM9

to

https://domain.co.uk/checkout/order-received/2786/?key=wc_order_i6GIKeD0SaNM9

Here is my htaccess code

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    # Checkout Page
    RewriteRule ^?elementor_library=single-product/order-received/?$    /checkout/order-received/$1 [R=301,L]

</IfModule>

CodePudding user response:

RewriteRule ^?elementor_library=single-product/order-received/?$    /checkout/order-received/$1 [R=301,L]

The RewriteRule pattern (first argument) matches against the URL-path only, not the query string, so this will never match the stated URL (which consists of a large query string and "empty" URL-path) - so does nothing.

You are also only matching part of the query string and since you are uisng an end-of-string anchor, it will never match the stated query string.

You are also using a $1 backreference but have no capturing subgroups in the RewriteRule pattern. So, $1 is always empty.

To match against the query string you need to use a RewriteCond directive and match against the QUERY_STRING server variable.

So ?elementor_library=single-product should be changed to checkout

This isn't the only thing you are doing in your example. You are converting part of the query string into a URL-path, but preserving the key URL parameter in the query string.

Try the following instead:

RewriteCond %{QUERY_STRING} ^elementor_library=single-product/order-received/(\d )/&key=(\w )$
RewriteRule ^$ /checkout/order-received/%1/?key=%2 [NE,R=301,L]

This will redirect a URL of the form /?elementor_library=single-product/order-received/<number>/&key=<code> to /checkout/order-received/<number>/?key=<code>

\w is a shorthand character class that matches the characters a-z, A-Z, 0-9 and _ (underscore), which appears to cover the value of the key URL parameter in your example.

Where %1 and %2 are backreferences to the capturing subgroups in the preceding CondPattern (RewriteCond directive).

The NE flag is just to ensure that the values copied from the URL string are not doubly URL-encoded (although this shouldn't happen with the example as posted).

You should test first with a 302 (temporary) redirect to avoid potential caching issues. And clear your browser cache before testing.

Also, the RewriteBase directive is not being used here and the <IfModule> wrapper is most probably redundant.

Note also, that the order of directives is important. This rule will likely need to go near the top of the root .htaccess file, certainly before the WordPress code block.

  • Related