Treehouse: Grow your CSS skills. Land your dream job.

A more in depth look at mod_rewrite .htaccess rules!

  • # September 26, 2011 at 3:14 pm

    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…

    # September 26, 2011 at 3:38 pm

    @standuncan – Where are you putting the file?

    # September 26, 2011 at 4:05 pm

    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.

    # September 26, 2011 at 4:13 pm

    So you have a redirect to that specific subdirectory?

    # September 26, 2011 at 5:16 pm

    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.

    # September 26, 2011 at 5:30 pm

    @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.

    # September 28, 2011 at 4:35 pm

    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]

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.