1

I've just finished setting up my first app on localhost - and the .htaccess working incredibly fine. I just uploaded the files to Google Cloud LAMP Server, set up the database & got everything working -- however, for some reason the PHP files are not being located (?) which is strange because it's working in localhost just fine, and HTML files seem to be rewrited just fine as well! Here's the error log in apache:

Negotiation: discovered file(s) matching request: /var/www/html/index (None could be negotiated).

Here's my .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

What is strange is that it's rewriting HTML with no problem but PHP files will throw this:

Not Found
The requested URL was not found on this server.

I tried pretty much everything:

  • Changed /etc/apache2/apache2.conf
  • Changed /etc/apache2/sites-available/000-default.conf
  • Made sure that 'a2enmod rewrite' is enabled

Still, getting

Not Found
The requested URL was not found on this server.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
duro
  • 35
  • 3

1 Answers1

1

If looks as if MultiViews is enabled, but .php files are not a permitted extension. MultiViews needs to be disabled for your mod_rewrite rules to be processed successfully. To disable MultiViews, add the following to the top of the .htaccess file:

Options -MultiViews

MultiViews is disabled by default, however, it is explicitly enabled (in the server config) on some distros.


Aside:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

The first condition that checks that the request does not map to a directory is entirely redundant.

The condition that checks whether the corresponding .php (or .html) file exists is not necessarily checking that the same "file" you are rewriting to. If, for instance, you requested /foo/bar and foo.php happened to exist then you will get a rewrite-loop (500 Internal Server Error). To resolve this issue, it should be rewritten like this instead:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]

The NC flag was superfluous, as was the anchors surrounding the RewriteRule pattern.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • I'm attempting to do this: Options Indexes FollowSymLinks Options -MultiViews AllowOverride All Require all granted However, still getting 'Not Found'. It's kind of stressing me out, probably I'm doing it wrong – duro Feb 01 '23 at 19:28
  • @duro What is the document root? Is there any more restrictive `` containers (perhaps in a `` block). Try adding some "nonsense" to the top of the `.htaccess` file - do you get an error? If not, then it looks like `.htaccess` files are not enabled. – MrWhite Feb 01 '23 at 19:33
  • .htaccess is enabled & it rewrites HTML files with no issue! This is out of my knowledge & can't find anything in Google for some reason. The closest I've been to seeing a solution was what you recommended with disabling MultiViews. I've added that line 'Options -MultiViews' to etc/apache2/apache2.conf & /etc/apache2/sites-available/000-default.conf, however with no success. – duro Feb 01 '23 at 19:36
  • 1
    `Options -MultiViews` can be added to the top of the `.htaccess` file itself. – MrWhite Feb 01 '23 at 19:40
  • Are you still getting the same "Negotiation" error in the error log? `MultiViews` is part of mod_negotiation and is associated with _content negotiation_. – MrWhite Feb 01 '23 at 19:42
  • 1
    Wow! That fixed it. If anyone has this same issue, just add `Options -MultiViews` at the top of your .htaccess – duro Feb 01 '23 at 19:46
  • @duro So, wherever you were adding it in the server config was not right place (something else was overriding it). `.htaccess` will override the server config in this case. (I've updated my answer to state where this should be added.) "``" - If you are requesting `example.com/index` (as confirmed in comments above) then it would seem your `DocumentRoot` is set as `/var/www/html`, in which case you should have a `` container that reflects this. – MrWhite Feb 01 '23 at 19:52