PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » HTAccess » Remove File Extention from URLs

Remove File Extention from URLs

RewriteRule ^about$ about.php [L]

That little bit will make http://example.com/about.php be able to load at http://example.com/about

Subscribe to The Thread

  1. Greg says:

    The following may be considered over the top, but I like to take it one step further by adding a trailing slash. If the slash is missing I feel like something is left… unfinished? Just a personal quirk I guess.

    This rule will match site.com/path/ and check to see if site.com/path.php exists. If the file exists it proceeds with the rewrite.

    RewriteCond /%{REQUEST_FILENAME}.php -f
    RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

  2. Dyllon says:

    This one seems a bit easier:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

    • This version breaks any urls with a dash ‘-’ between words.

      Although that said Greg’s seems to as well…

    • Dyllon says:

      I’ve never encountered that problem. In fact, I just tried it and it worked fine.

    • Richard Ellis says:

      Having tried all the examples above,

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

      I am unable to see any change to my site URL’s.

      The suffix is still visible when I reload the page.

      Any ideas on how I can correct this?

    • mod rewrite works only on apache server.
      before trying please be sure that you are working on apache server and the mod rewrite module/extension is enabled.

  3. Rajasekar says:

    i would like this snippet, but how can use this snippet? where i use it? anyone explain this deeply?

  4. elandy2009 says:

    A simple thing: This rule is inserted in an Apache Server .htaccess file. If you don’t have this file, you must create it. And this requires your host has activated the rewrite module.

  5. Christian says:

    Hi! I have a question about URL rewrite. I use a dynamical php file to load my posts as usual and I use the rewrite in the form: mydomain.com/id_title-of-the-post , so as you can guess, the real URL is something like mydomain.com/file.php?id=XX , and I look in my database for the post with id=XX.

    In that case, the only real thing that matters in the URL is the “id” part. You could type mydomain.com/id_another-stupid title-of-the-post, and you will get to the same post.

    But I’ve seen that, like in your website, your URLs are in the form css-tricks.com/category/title-of-the-post ….you don’t use an ID anywhere so the only thing I can think right now is that your DB query looks for the post where title field=title of the post.

    Am I right or i’m talking BS? I can make that query too but I thought that looking for just an ID is way much faster for the database.

    This is all under the assumption that you also use a dynamical file that loads your posts and each one is not an independent html file.

    I would really appreciate if you can shed me some light on that matter. Thank you very much! (I really like your web ^^). Greetings from Peru =]

  6. Ashley says:

    This is awesome, I had no idea you could do that. Will be doing it on my sites later on :)

  7. Jeff says:

    Dyllons method worked for me. Thanks for that, been looking around and no other method that I found worked for me.

  8. Boris says:

    Hello Chris and everyone.
    My first post here. Thank you for all the great tips! I’d like to share my favorite version of the script. It does not add a trailing slash unless you visit a index file. Remember to remove the “.php” from your href links.


    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^([^\.]+)$ $1.php [NC,L]

  9. joe says:

    Hi everyone

    A question. I use the trick and works perfect with files in the main root
    RewriteRule ^about$ about.php [L]
    my problem is with subdirectory files
    RewriteRule ^/path/about$ /path/about.php [L]
    can someone help me to use the trick?

  10. surendhiran says:

    Hi
    Remove File Extention from URLs is not working in my site.. Can you give some tips to solve the proble

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~