qadenza
qadenza

Reputation: 9293

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

Upvotes: 4

Views: 5281

Answers (2)

Phan Hero
Phan Hero

Reputation: 325

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]

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133428

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]

Upvotes: 5

Related Questions