treehouse : what would you like to learn today?
Web Design Web Development iOS Development

A more in depth look at mod_rewrite .htaccess rules!

  • I have researched until my brain hurts numerous times. Removing the extension in the browser should rather be an easy rewrite rule. I have found four different ways and yet again, none of them are working.

    Granted, the current site is hosted on godaddy, however I have used one of these methods before on this same hosting and it has worked before, however only some of the time?

    Yes it is an Apache server.
    Yes mod_rewrite is enabled.
    Yes I am removing the extension in the href links.


    First and most common method I believe, not working:



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



    Second method with a failed attempt:



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



    Third way which is failing:



    RewriteRule ^index$ index.php [L]
    RewriteRule ^page$ page.php [L



    And lastly, I think is the correct way since it actually rewrites the link appending the extension, however still failing (this is the method that has worked before):



    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]



    Any help greatly appreciated...
  • @standuncan - Where are you putting the file?
  • I have the .htaccess file at the root level, for this site.

    However, this site is in a sub-directory of a shared hosting account with a separate domain directing to this sub-directory. I know the .htaccess file will only affect what is at the same or within that level.
  • So you have a redirect to that specific subdirectory?
  • Maybe I'm explaining it wrong, it's setup in the hosting panel that this sub-directory is the root folder. Not a true redirect.
  • @standuncan - I'm totally confused. You have the .htaccess at the very root level yet have the site in a subdirectory. Is that what you're saying?

    If not, take a screenshot of your file structure.
  • Well I kept messing with it, but I couldn't get it. So I just swapped the extensions to .html instead of .php using:



    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)\.html$ /$1.php [L,QSA]