Search code examples
php.htaccessmod-rewriteplesk

.htaccess hide page extension


Whenever I attempt to hide the .php extension with .htacess and go to a page using the .php extension all that is displayed on the screen is "Page Not Found". I had it working perfectly before, but I have recently changed hosting companies and now I am using Plesk Onyx 17.0.17 with CentOS Linux 7.3.1611 and PHP 7.1.4.

Here are the contents of my .htaccess file:

Options -Multiviews -Indexes

# Disable Automatic Directory detection
DirectorySlash Off

ErrorDocument 404 /!404.php

RewriteEngine On
RewriteBase /

# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/+$ $1 [R=301,L]

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

Solution

  • I have it working now thanks to Anubhava's answer on this question. I still have no idea why my old could would not work though.

    Here is the code that I used to solve the problem:

    ## hide .php extension
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R,L,NC]
    
    ## To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [L]