1

I have a simple database query script alongside with my WordPress installation, to which I pass a parameter using the following URL: http://example.com/db/?p=foo.

My database script reads the parameter with

$pid = $_GET['p'];

Everthing works fine, IF the parameter does NOT START WITH A NUMBER.

so, ?p=foo is ok, but ?p=3poo is EMPTY.

Edit: I confirmed this by changing my script to

<?php
print_r($_GET);
exit;

which gives as an output

Array ( )

I suspect that it's the mod_rewrite who suppresses the parameters starting with a number. But I have no clue why, and how I can change this behavior. Any ideas?

Edit: Unfortunately, I can not change the name of the parameter 'p', nor can I change the values of the parameter so they do not start with numbers, since the URLs where released as QR-tags to the public already, so I need a workaround to make this exact URLs work.

Here is my .htaccess file in the root folder (edited as suggested by MrWhite, works as expected)

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

</IfModule>
9
  • 1
    If it is mod rewrite that's doing this it would be an Apache question, but as long as you are using the standard WP rewrite rules it shouldn't be an issue, but keep in mind that p is a query var, it is not safe to use for arbitrary things and carries meaning, and gets passed to the main WP_Query, you should avoid it for arbitrary parameters ( and you should look into the REST API, which probably does what your db script is trying to do already )
    – Tom J Nowell
    Commented Apr 10, 2018 at 21:26
  • Thanks Tom. I wasn't aware of special meaning of p. My problem is that I hard printed and distributed already the URLs, so unfortunately can't change, neither p, nor the fact that I'm starting with numbers ... thats why I desperately try to find a fix. (if-not, I would just change the parameter values not starting with a number).
    – nounours
    Commented Apr 10, 2018 at 21:32
  • If you're directly accessing GET it should not be an issue, something must be interfering with the $_GET array, you will need to check your theme and plugins
    – Tom J Nowell
    Commented Apr 10, 2018 at 21:38
  • Also, how are you confirming this issue? e.g. are you testing the result of the script? Or are you outputting the p value directly, e.g. var_dump($_GET);exit;? Have you confirmed this outright? If so edit your question to demonstrate what you did
    – Tom J Nowell
    Commented Apr 10, 2018 at 21:41
  • Just to confirm... this has nothing to do with mod_rewrite. The .htaccess code you posted above doesn't do anything with the query string. Aside: Why have you duplicated the WordPress front-controller? The chunk of code after # BEGIN WordPress isn't actually doing anything!?
    – MrWhite
    Commented Apr 10, 2018 at 23:41

2 Answers 2

2

Thanks to Tom, I looked more carefully to the plugins.

And I can confirm that the problem is NOT RELATED to mod_rewrite, but to a plugin conflict. (sorry that I posted such a suspicion, but I read somewhere a very similar problem that was explained like that.)

Deactivating plugin by plugin, I found that one you made this behaviour. Still no clue why, but at least I have now a workaround: deactivating this plugin.

Thanks Tom and all the others for your help!

1
  • Sometimes, finding out that you are asking the wrong question is the answer. Commented Apr 11, 2018 at 19:11
1

Finally, completely understood and solved the problem using @MrWhite 's advice to use mod_rewrite to rewrite the query. Thanks!

To explain:

1) My http://example.com/db/?p=foo is (and this I didn't realise) actually not accessing the dbquery.php script, but a wordpress-page "db", which calls the dbquery.php script using the Plugin "INCLUDEME".

2) So, the query-string is first processed by Wordpress (or whatever, don't know the right term), and since - as @Tom J Nowell pointed out - my parameter "p" has a special meaning, it was emptied by "Wordpress", and already no query string made it to the plugin. So it was not the plugins fault.

3) Conclusion: not mod_rewrite, not the plugin, not the value starting with a number was the problem, but the simple fact that I used "p" was the source of the problem.

4) The solution consisted in:

a) rename the WP-page "db" into "nachverfolgbarkeit"

b) create a directory called "db"

c) place a .htaccess file in db containing the below redirection, to redirect from db to the new page "nachverfolgbarkeit" (which calls dbquery.php using INCLUDEME); and correcting the parameter from "p" to "pid"

Not very nice patch, but it works well. So my advice to every reader: "Never use p as a parametername" ...

Thanks to all for their help. Nounours

RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^$ /nachverfolgbarkeit/?pid=%1   [R=301]

Not the answer you're looking for? Browse other questions tagged or ask your own question.