How to hide the .php extensions from the Url

How to hide .php extension.

I have already applied a rule in .htaccess.

Now the information in url can display with our without php extension. But am looking for some code that i can hide the extension so users cannot see it.

Can someone can help me out.

continued from How to change the page title and description when .header.php included

Try this

# Run Php without filename extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

# Return 404 if original request is .php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

Would the first set - the $1.php not be a Request ?

How does the second set recognize an “original” Request vs a rewritten one?

What rule have you got?

If you have the right rule there should be nothing else to do.

1 Like

I tried with this cond
when i switch my cursor on a page it shows was not found.
But when i remove my .php extension the page display.

But when a user clicks on some page it will show the same. How to redirect even if ,php comes an extension.
can you help.

May be this:
RewriteEngine on
RewriteBase /
RewriteRule ^(.+).php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
U need to set mutliviews if so add this to the top of the htaccess file.
Options +MultiViews

@rickrocky33 Try this:

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

This works fine for me.

If this doesn’t work, then clear your browser cache and data and try again or use incognito mode.

Mr Tom,

your rule worked fine, thanks a lot. helping the beginers like me.

Started the day with great smile with your coding.

Thanks, Thanks, Thanks:slight_smile:


Great! :wink: u r welcome

Hi Tom,

yes worked fine, not encountered a problem.

My site has carte option. after redirecting the items am not able to add to carte.

when i add the products AND click go to cart button.

The page display in full white. can you help me with this.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.