0

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>
Fury
  • 4,643
  • 5
  • 50
  • 80
  • What’s the issue, exactly? You haven’t included any clear problem statement. See [ask] – esqew Jun 29 '22 at 13:57
  • Still no clear problem statement. What happened instead? Why did you specifically expected that and what happened otherwise? (only stating what you want and then dropping some code that could be easily just copy and pasted does not make your prorgramming / configuration intend clear - it can help but also invite to guesswork). – hakre Jun 29 '22 at 14:08
  • @hakre I don't think you have made enough effort to read the question. How the others can read and write the correct answer but you can't. It is very simple just to change a part of a string in the url – Fury Jun 29 '22 at 15:06
  • I beg your pardon, this is not about your question alone, but also to better differentiate your question specifically from the existing Q&A material. If you please so to only read it broadly, let me suggest existing material. – hakre Jun 29 '22 at 15:09
  • Does this answer your question? [.htaccess rewrite query string as path](https://stackoverflow.com/questions/13003319/htaccess-rewrite-query-string-as-path) – hakre Jun 29 '22 at 15:10
  • Thank you for the advise. But the one you suggested is more generic. I still would prefer below answer. However I think this is not going to work for me (as I've been advised our server runs by nginx ) but I am pretty sure it will for someone else. – Fury Jun 29 '22 at 17:17

1 Answers1

2
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.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Thank you @MrWhite I've been advised our server runs by nginx. Unfortunately I can't even use or test the above – Fury Jun 29 '22 at 17:18
  • @Fury "our server runs by nginx" - Ah, then you'll need access to the server config and do this the Nginx way. `.htaccess` / mod_rewrite does not apply in that case. – MrWhite Jun 29 '22 at 18:39