Remove File Extension from URLs

So you want Pretty URLs?

You have a site with index.php or index.html files and you want to create friendly URLs. A simple script I have been working on now for a few years this file is a .htaccess file and must live in the root of your domain so for example

Just as an FYI for anyone using or editing this, if you do not know what this is then check out more about friendly URLs and the concept of mod rewrite before adding this. If you experience and 500 errors or anything like that please remove the script.

The Code

AddType text/x-component .htc
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

What it Does?

This handy script will remove all .php extensions creating lovely SEO and human friendly URLs for your static site, it will also not mess up any exisiting folders that you have live on your domain.

Questions, comments or amends please let me know here.


  1. Ashutosh
    May 30, 2015 at 4:34 am

    I had tire ur’s code it work fine for all the pages with extension , but i have one problem. is that how to remove the link details like:-
    i want to change the above one to this:-
    if u can help me then i m very great full to u.
    Thanks bro.

  2. whyteman
    July 19, 2015 at 1:12 pm

    thanks for your post, but i’ve tried this but its not working, i’m using a localhost for it for a test, when i try it changes my address from “localhost/sitefolder/index.html” to “localhost/index/”

    how do i correct it.

  3. whyteman
    July 19, 2015 at 1:41 pm

    Thank you niallflynn, i just copied and pasted your code, did some little edit and it’s working fine now.. woooow.. thanks alot. your post helped.. i’m grateful. 😀

  4. Anneke
    November 14, 2015 at 10:32 pm

    I tried your code, and it works, but not for gastenboek. The main file is in the root, but the captcha, messages and send are in a folder. When I click on Gastenboek, I get 404 error page.
    How can I fix this problem?

  5. Subrata Chowdhury
    December 6, 2015 at 10:26 am

    I want to remove the file-name with extension from my address bar’s URL. ie:- should look like in address bar. Thanks in advance.

  6. riahi
    December 7, 2015 at 10:35 pm

    Hi freind
    i’m tottaly newby in html and css and i have a site copied from freind
    my problem that i have 6 php file getting out message to visitor and i dont want to let visitors know entire url of php file
    can you please help me on this step by step

  7. Ebby Hesse
    December 31, 2015 at 12:16 am

    This is the BEST code option that has worked after trying all on the web. It did what I wanted even in sub folders….WOW. Thank s buddy.

  8. Niall Flynn
    August 17, 2016 at 2:07 am

    Glad it helped to all others ????

  9. Raul
    August 22, 2016 at 5:02 pm

    It works, but it does not generate duplicate content?


    Of the two forms can be accessed. Does Google takes this as duplicate content?

  10. Niall Flynn
    August 22, 2016 at 7:51 pm

    Your a little confused here, but understandable, a 301 redirect is a permanent note to the SE Google or otherwise that this is a redirect. Duplicate content is not relevant in this case, that said the use of a correct canonical tag, an accurate XML site map etc and the correct use of internal canonical link references will help here. I would be far more wary of duplicate content from a sidebar, footer point of view.

  11. Niall Flynn
    August 22, 2016 at 8:04 pm

    Anyone having localhost issues, should really consider developing this sort of stuff on live servers, lcoal host great for dev etc. Your web server and local host could be very different so in many cases I think I would opt for live dev where possible or at least a web server with the same config as your live server. Many shared and cheaper hosts too have odd configs so it can vary.


Leave a comment