1

Background: We use an external service that redirects to a confirmation page on our Wordpress website. This service sends certain parameters via URL-strings that we want to remove, as they contain private information. Unfortunately, the service is not configurable in that direction (i.a. it will always send the full set of params).

Objective: I am currently working on an nginx location block that is supposed to strip certain URL-Parameters from the requests to a certain page, and redirect to the same page with a "clean" URL. So far, I have managed to get rid of the unwanted param in the URL, however, I am struggling with the rewrite-part of the location block. It seems that I have not yet figured out how to rewrite the URL correctly in a Wordpress context.

Error description: When accessing the desired URL with the param ?full_name=john, nginx correctly strips the param and tries to redirect to the page. However, nginx throws a 404 error.

Environment:

  • Plesk Onyx 17.8 on Ubuntu 16.04
  • Wordpress 5.2.2
  • nginx: Plesk's sw-nginx
  • PHP: 7.3.6 (FPM via nginx)

Code: I used Siwei Shen's comment on Remove parameters within nginx rewrite to start off. Here is what I have so far:

location ^~ /confirmation/ {

    if ($request_uri ~ "([^\?]*)\?(.*)full_name=([^&]*)&?(.*)") {
        set $original_path $1;
        set $args1 $2;
        set $unwanted $3;
        set $args2 $4;
        set $args "";

        rewrite ^(.+)$ /index.php/$original_path?$args1$args2 permanent;
    }
}

I believe the culprit is rewrite ^(.+)$ /index.php/$original_path?$args1$args2 permanent; as I am most unsure about how to rewrite this Wordpress-friendly.

I'd greatly appreciate any help or pointers in the right direction. Big thanks in advance!


UPDATE 2019-07-05:

I have altered the code thanks to @RichardSmith:

location ^~ /confirmation/ {

    if ($request_uri ~ "([^\?]*)\?(.*)full_name=([^&]*)&?(.*)") {
        set $original_path $1;
        set $args1 $2;
        set $unwanted $3;
        set $args2 $4;
        set $args "";

        rewrite ^(.+)$ $original_path?$args1$args2 permanent;
    }
}

This causes a 404-error (taken from proxy_error_log):

2019/07/05 11:51:13 [error] 22623#0: *39709 "/var/www/vhosts/domain.de/sub.domain.de/confirmation/index.html" is not found (2: No such file or directory), client: 109.41.XXX.XXX, server: sub.domain.de, request: "GET /confirmation/ HTTP/2.0", host: "sub.domain.de"

This looks like intended behavior, as there is no index.html in that location. However, now I must tell nginx not to append the index.html to the rewritten request.

Do you have an idea how to accomplish this? Thanks in advance!


SOLUTION:

The solution to my Problem was that I was missing a statement to tell nginx how to work when there is no file found. Therefore, nginx was looking for the index.html in that place and could not find it. Adding an additional if()-block after the URL rewrite that handles location access without a file did the trick.

Code:

location ^~ /confirmation/ {

    if ($request_uri ~ "([^\?]*)\?(.*)full_name=([^&]*)&?(.*)") {
        set $original_path $1;
        set $args1 $2;
        set $unwanted $3;
        set $args2 $4;
        set $args "";

        rewrite ^(.+)$ $original_path?$args1$args2 permanent;
    }
    if (!-e $request_filename) {
        rewrite / /index.php last;
    }
}
hpsi
  • 36
  • 5
  • You are putting `index.php` in front of the URI, then appending the original request as path_info. If the original request is already handled correctly, albeit with private info, perhaps you should just remove the `/index.php/` part from your rewritten URI. – Richard Smith Jul 04 '19 at 14:28
  • Thanks for the pointer, @RichardSmith! I have now tried a little more and I have found the following error in proxy log: `2019/07/05 11:51:13 [error] 22623#0: *39709 "/var/www/vhosts/domain.de/sub.domain.de/termin-bestaetigt/index.html" is not found (2: No such file or directory), client: 109.41.XXX.XXX, server: sub.domain.de, request: "GET /termin-bestaetigt/ HTTP/2.0", host: "sub.domain.de"` So nginx is trying to append /index.html to the request, which cannot work, so the 404 is correct so far. Now I would need to know how to stop this behaviour... – hpsi Jul 05 '19 at 09:52

1 Answers1

0

SOLUTION:

The solution to my Problem was that I was missing a statement to tell nginx how to work when there is no file found. Therefore, nginx was looking for the index.html in that place and could not find it. Adding an additional if()-block after the URL rewrite that handles location access without a file did the trick.

Code:

location ^~ /confirmation/ {

    if ($request_uri ~ "([^\?]*)\?(.*)full_name=([^&]*)&?(.*)") {
        set $original_path $1;
        set $args1 $2;
        set $unwanted $3;
        set $args2 $4;
        set $args "";

        rewrite ^(.+)$ $original_path?$args1$args2 permanent;
    }
    if (!-e $request_filename) {
        rewrite / /index.php last;
    }
}
hpsi
  • 36
  • 5