PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 2, 2022

[FIXED] How to fix 404 Error after modifying nginx $args and redirecting to modified URL in Wordpress

 September 02, 2022     nginx, nginx-location, nginx-reverse-proxy, url-rewriting, wordpress     No comments   

Issue

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;
    }
}

Solution

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;
    }
}


Answered By - hpsi
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing