Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

htaccess - remove .php extension from url


I tried a lot of code to remove .php from url
for example - ht.abuena.net/presto.php -> ht.abuena.net/presto
and vice versa - internally

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

nothing works - the url stays unchanged
page is reloading by right clicking on the button Reload and choosing - Empty Cache and Hard Reload - so I hope the cache is cleared

live example here - here


Solution

  • With your shown samples, please try following htaccess rules file.

    Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteBase /
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    ##using THE_REQUEST variable for condition check.
    RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
    ##Performing external redirect here.
    RewriteRule ^  %1? [R=301,L]
    
    ##Performing rewrite for non-existing pages.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*)/?$ /$1.php [QSA,L]