0

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)

JoSSte
  • 2,953
  • 6
  • 34
  • 54

1 Answers1

0

After meditating on the issue, and practicing some Google fu, I stumbled upon this question on SO.
The culprit is Apache httpd's Multiview feature, which, if there exists no file with the asked for name will look at the basename of the files that exist on the server, and serve one of those instead.

Adding Options -Multiviews to the .htaccess file solves the issue.

Update

You may not have permission to override this setting in a .htaccess file. If you do have access to the vhost configuration, check that the <directory> Options don't have multiview in them.

JoSSte
  • 2,953
  • 6
  • 34
  • 54