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

[Solved] hide .html with .htaccess not working - Please Help

  • Hello!

    I am attempting to hide the .html file extension in the url - and redirect when manually typed in. I am using the .htaccess to accomplish this, however I am not seeing any results. Any help is much appreciated. Thank You.

    The code:



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

  • Did you set the base and turn the engine on before hand?




    RewriteBase /

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

    Thanks for the reply. I was able to solve the problem by changing the structure of the page links in html.

    <a href="about.html">About</a>

    became
    <a href="about">About</a>


    I am still having one issue though. I would like to redirect users if they type the .html extension in the url.

    I have attempted solving this by changing the .htaccess code to the following...with no luck. A 404 Error occurs. Any ideas?

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\.html$ /$1 [L,R=301]


  • Anybody have a suggestion?
  • change the extensions to PHP...It is much easier to accomplish that way.
  • Unfortunately I am not well versed with PHP so i would like to stick with html for now…
  • @NSR

    Here is an answer from stackoverflow.com:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.*)$ $1.html [NC,L]


    If the person tried adding a .html extension to the URL, wouldn't it automatically be removed with the code above?
  • Hey all!

    Thanks for the replies. I attempted @ChristopherBurton's solution…The .html extension is initially removed, however if the user types it in the url…it appears again.

    ANy Ideas?
  • Also…

    I am using the code below to hide the index.html…which seems to work correctly.

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
    RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]
  • Howdy!

    I have solved the problem. For anybody interested…this is how it works.

    RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\  [NC]
    RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^(.+)\.html$ /$1 [R=301,L]

    RewriteCond %{SCRIPT_FILENAME}.html -f
    RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
  • @NSR
    Would you name the file name.htaccess in order for the file to work?
  • @NSR, this is a little off subject but i was wondering what would you be using this for? The reason i am asking because i have seem methodology before but im only a college student and havent really seen any implementation for this. So basically, what real world scenario are you trying to achieve by removing the extension?

    Good luck with finding the answer btw.
  • Well the code that Chris wrote in the PHP snippets works just fine, but you have to link to /about rather than /about.html or something
  • i want to hide url in subdirectory www.123musiq.biz/folder/page.html to www.123musiq.biz/folder/page

    .htaccess tag added in subdirectory but it doesn't work. the tag is RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.html -fRewriteRule ^(.*)$ $1.html what can ido? what is the code for hide url extension in subdirectory file?

  • Hello all user i want hide page how can i do? this my url http://khmerera.com/detail.php?ads=$id but now i want hide detail.php how can i do it i want have like this **http://khmerera.com/?ads=$id **please help me Thz!

  • @8lest the file name is simply .htaccess nothing before the period

    @John I wanted to remove the extension to clean my urls. I was working with static html files.

    Prior to removing the extension, the url would read website.com/about.html.

    With the extension removed, it would look like website.com/about. Hope that was clear.

  • Basically it makes them look "pretty".

  • @NSR:

    You're thinking about this a little backwards. You should be using .htaccess to transparently redirect the user to the real file when the file extension is left off:

    /file => /file.html

    This keeps the URLs "pretty" (the user never sees the .html) while keeping everything working.

    In order to "remove" the file extensions from the URL, you should simply remove them: don't type /file.html, just type /file.

    From some of your earlier comments, I thought you'd figured that out:

    I was able to solve the problem by changing the structure of the page links in html.

    but maybe there's still some confusion?

    If the user types in whatever.html, there's no real point in removing it for them via .htaccess - if it works anyway, why "fix" it?

    -- If the user wrote it, it's obviously not "unfriendly" to them.

    -- Search engines see what's written in hyperlinks, not what users type in their address bar. As long as your hyperlinks are "pretty," everything is fine.

    At best, you're just adding two or three unneeded HTTP requests per page view.

  • @traq

    Thanks for the response. I resolved the issue quite some time ago — just wanted to respond to a few users with questions regarding similar issues.

  • okay, no problem. I wasn't sure.

  • @Rugg: Hi Rugg, I follow your code below

    RewriteCond %{THE_REQUEST} \ /(.+/)?index(.html)?(\?.*)?\ [NC] RewriteRule ^(.+/)?index(.html)?$ /%1 [R=301,L]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^(.+).html$ /$1 [R=301,L]

    RewriteCond %{SCRIPT_FILENAME}.html -f RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

    and it works fine. However, because I use 2 files for contact page which is contact.html and contact.php When I type /contact the html page cannot load.

    Do you know why does it happen and how to fix it? Thanks a lot.

  • **I there I Am new with httacces **

    Extension .html but i can hide using this rule .. help