Thursday, March 26, 2020

WordPress Some URL parameters are empty on POST or GET

How to fix empty URL Parameters on Post or Get in WordPress

Parsing URL parameters in WordPress and they are appearing empty it's most likely an issue with your .htaccess file.

Start with default WordPress .htaccess file from https://wordpress.org/support/article/htaccess/

Add the following line  RewriteRule "/users/([0-9]+)/?" "/users?page=$1" [QSA]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# BEGIN WordPress

RewriteEngine On
RewriteBase /

RewriteRule "/users/([0-9]+)/?" "/users?page=$1" [QSA]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

The key to this fix is the QSA flags, see below for full explaination. 

Understand Apache RewriteRule Flags - http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa


QSA|qsappend

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
Consider the following rule:
RewriteRule "/users/([0-9]+)/?" "/users?page=$1" [QSA]
With the [QSA] flag, a request for /users/123/?fullprofile=true will be mapped to /users?page=123&fullprofile=true
Without the [QSA] flag, that same request will be mapped to /users/123/?fullprofile=true - that is, the existing query string will be discarded.


No comments:

Post a Comment