1

I have an apache2 installation on my local linux server. It has a virtual host called pcts.local which has the root /var/www/repos/pcts/. Inside the root of pcts.local is a .htaccess file which attempts to rewrite urls to include .php if it isn't given like below:

http://pcts.local/ -> http://pcts.local/index.php
http://pcts.local/contact -> http://pcts.local/contact.php

The problem is, http://pcts.local/contact gives an error 404 but http://pcts.local/contact.php gives 200.

Virtual Host Configuration:

<VirtualHost *:80>
        ServerName pcts.local
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/repos/pcts

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

.htaccess file in /var/www/repos/pcts/

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [NC,L]

Thanks in advance of any help!

Cobain Ambrose
  • 91
  • 1
  • 1
  • 8

2 Answers2

1
<VirtualHost *:80>
    ServerName pcts.local
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/repos/pcts

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

If this is your complete config then your .htaccess file is not being processed.

You've not enabled .htaccess overrides for the specific directory. (ie. You've not enabled the parsing of .htaccess files.) .htaccess overrides are disabled by default.

However, you've not enabled access to this area of the filesystem either? Have you done this elsewhere in the server config?!

You should have a relevant <Directory> section like the following inside the <VirtualHost> container:

<Directory /var/www/repos/pcts>
    # Enable .htaccess overrides
    AllowOverride All

    # Allow user access to this directory
    Require all granted
</Directory>

You can restrict .htaccess overrides further if required (see the reference link below)

Reference:

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Thank you, I think this has solved the issue because now I'm getting an error 500 (internal server error) which is probably due to the PHP file itself. – Cobain Ambrose Dec 02 '22 at 20:19
  • 1
    @CobainAmbrose Have you enabled/installed mod_rewrite? This is an additional Apache module, not installed by default. If mod_rewrite is not installed then you will also get a 500 error. Check the web server's error log for the details of the error. – MrWhite Dec 02 '22 at 21:50
  • 1
    Thanks for this, the module was not enabled and now the 500 error fixed :) – Cobain Ambrose Dec 02 '22 at 22:05
0

In your code, REQUEST_FILENAME is expecting a file with a php extension to perform the rewrite.

Try this instead:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
kissumisha
  • 484
  • 4
  • 12
  • I tried this just now but I'm still getting a 404 I'm afraid. – Cobain Ambrose Dec 02 '22 at 16:08
  • "REQUEST_FILENAME is expecting a file with a php extension" - That doesn't really make sense. Changing the second condition to `%{REQUEST_FILENAME} !-f` is redundant since the `RewriteRule` _pattern_ `^([^\.]+)$` is very unlikely to match a file anyway. The OPs original condition is "OK" since it's testing whether the `.php` file exists before rewriting the request. – MrWhite Jan 03 '23 at 16:48