Home > other >  htaccess - remove .php extension from url
htaccess - remove .php extension from url

Time:12-20

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

CodePudding user response:

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]

CodePudding user response:

In my case if the project is core PHP one, I use the below lines in .htaccess file:

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