TL;DR
If I have a /campaign.php
file /campaign/some-url-stuff
matches it instead of using the rewrite rule in .htaccess
I was just making a script for an easter campaign and called it campaign.php
I set it up to handle a GET
parameter name
so sample URL: https://localhost/campaign.php?name=Easter2021
Then I went to my .htaccess
file (apache 2.4 on windows) and created a line in that:
RewriteRule ^campaign/(.+)$ /campaign.php?name=$1 [L]
Validating with this debugger that is was correct to call it with the following URL: https://localhost/campaign/Easter2021
Then I added some error handling to my script:
MCVE
<?php
$campaignName = filter_input(INPUT_GET,'name',FILTER_SANITIZE_STRING);
if ($campaignName == 'Easter2021') {
echo "OK";
} else {
header("content-type text/plain");
echo "Invalid campaign Requested [$campaignName]";
print_r($_GET);
}
So if my file is called the same as the root of the pretty URL, it seems like it is called instead, and the rule is not matched, which means that I get no name
GET parameter.
print_r($_GET);
returns Array ( )
But if I rename the file and update my .htaccess
to this: RewriteRule ^campaign/(.+)$ /campaignHandler.php?name=$1 [L]
Then https://localhost/campaign/sdfdsfsdf
gives Array ( [name] => sdfdsfsdf )
And the question is: Why? and what can I do to avoid this in the future? (there must be some php/apache config to solve this)