Create a file at the root of your web server called “.htaccess”. Put the following code inside:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* $0.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* $0.php
This will make it so you can access all your PHP pages without having to type the “.php” extension the URL or your <a href>s. i.e., you can access “yoursite.com/about.php” just by going to “yoursite.com/about”. The actual file names should still have the .php extension though. You can do this with whatever file type you like too.
8 thoughts on “Use .htaccess to hide file extensions”
thx 4 share
adnan
i want to remove the file extension form the subdirectory file
http://www.visitorsinduct.com/folder/cool.html to http://www.visitorsinduct.com/folder/cool
how it posible and what is the code?
drench
Code is exactly the same as in my post, except you need to replace both instances of “php” with “html”. You also must be running Apache on your server with mod_rewrite enabled/installed.
Mark
i used the code above and i can now go to http://www.adiamondclean.co.uk/index and it works the same as with the .html at the end but when i click on my page tabs at the top i.e. contact us it goes to the full url with /contact.html at the end so why is this how do i stop this from happening without having to go into each page and take off the html extension in the code? is there a code that works in the .htaccess file to make this work properly?
thanks [email protected]
steven
@steven: You really should go into each page and fix the extensions, but I believe you can redirect them by adding another rule,
Mark
I want to hide the .php extension.
I have tried your code but its not working.
Can you help for it.
Sunil Kumar
@Sunil: No. You need to provide more detail. I suggest you ask on stackoverflow.com.
Mark
http://localhost/hides/services
http://localhost/hides/about
to
http://localhost/hides/services/
http://localhost/hides/about/
shahid