Get a free trial // Grow your CSS skills // Land your dream job

hide .html with .htaccess not working – Please Help

  • # April 21, 2012 at 8:53 pm


    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:

    <br />
    <br />
    RewriteCond %{REQUEST_FILENAME} !-d<br />
    RewriteCond %{REQUEST_FILENAME}.html -f<br />
    RewriteRule ^(.*)$ $1.html<br />
    <br />
    # April 22, 2012 at 2:20 pm

    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
    # April 22, 2012 at 5:13 pm


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




    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]
    # April 23, 2012 at 5:21 pm

    Anybody have a suggestion?

    # April 23, 2012 at 8:30 pm

    change the extensions to PHP…It is much easier to accomplish that way.

    # April 24, 2012 at 10:19 pm

    Unfortunately I am not well versed with PHP so i would like to stick with html for now…

    # April 25, 2012 at 10:47 pm


    Here is an answer from

    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?

    # May 1, 2012 at 11:52 am

    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?

    # May 1, 2012 at 12:02 pm


    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]
    # May 1, 2012 at 3:19 pm


    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]
    # August 27, 2012 at 9:05 pm

    Would you name the file name.htaccess in order for the file to work?

    # August 27, 2012 at 9:56 pm

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

    # August 28, 2012 at 12:25 am

    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

    # September 15, 2012 at 12:42 pm

    i want to hide url in subdirectory

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

    # September 27, 2012 at 3:56 am

    Hello all user i want hide page how can i do? this my url **$id** but now i want hide detail.php how can i do it i want have like this **$id **please help me Thz!

Viewing 15 posts - 1 through 15 (of 24 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed