Search code examples
apache.htaccessmod-rewrite

Remove .php extension with .htaccess


Yes, I've read the Apache manual and searched here. For some reason I simply cannot get this to work. The closest I've come is having it remove the extension, but it points back to the root directory. I want this to just work in the directory that contains the .htaccess file.

I need to do three things with the .htaccess file.

  1. I need it to remove the .php

a. I have several pages that use tabs and the URL looks like page.php#tab - is this possible?

b. I have one page that uses a session ID appended to the URL to make sure you came from the right place, www.domain.example/download-software.php?abcdefg.

Is this possible? Also in doing this, do I need to remove .php from the links in my header nav include file? Should IE "<a href="support.php">support</a>" be <a href="support">support</a>?

  1. I would like it to force www before every URL, so it's not domain.example, but www.domain.example/page.
  2. I would like to remove all trailing slashes from pages.

I'll keep looking, trying, etc. Would being in a sub directory cause any issues?


Solution

  • Gumbo's answer in the Stack Overflow question How to hide the .html extension with Apache mod_rewrite should work fine.

    Re 1) Change the .html to .php

    Re a.) Yup, that's possible, just add #tab to the URL.

    Re b.) That's possible using QSA (Query String Append), see below.

    This should also work in a sub-directory path:

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]