1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Remove .html from URL with .htaccess?

Discussion in 'HTML & Website Design' started by Web_Dev_Chris, Dec 20, 2016.

  1. #1
    Hi,

    I have been trying to figure out how to remove the .html from the url as it's quite unattractive to see the .html extension at the end of the links. I don't think I have come across a single website that has .html anymore.

    I have googled how to do this and the only thing that seem to come up is .htaccess? Is this the best method to go about this or would you recommend something different.

    Do you have a reliable source or tutorial reference that I could follow?


    Thanks,

    Regards,
    Chris
     
    Web_Dev_Chris, Dec 20, 2016 IP
  2. badger_

    badger_ Greenhorn

    Messages:
    52
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    20
    #2
    Why don't you create index.html in each subdirectory? this way the server won't need to do the rewrite in every request. I mean this:

    /index.html
    /foo/index.html
    /foo/bar/index.html
    /wu/index.html

    So you have these URLs:

    http://example.com
    http://example.com/foo
    http://example.com/foo/bar
    http://example.com/wu

    You can do 301 redirects for the URLs ending in /index.html... surely there is a rewrite rule for this. I don't know if the best practice is doing this or using canonical.
     
    Last edited: Dec 20, 2016
    badger_, Dec 20, 2016 IP
  3. Web_Dev_Chris

    Web_Dev_Chris Well-Known Member

    Messages:
    222
    Likes Received:
    12
    Best Answers:
    1
    Trophy Points:
    105
    #3
    Do people actually do this? Will if negatively impact SEO?
     
    Web_Dev_Chris, Dec 20, 2016 IP
  4. badger_

    badger_ Greenhorn

    Messages:
    52
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    20
    #4
    Most sites run with PHP so there is no need for the index.html file in each directory.

    The only problem I see with this approach is the possibility of duplicate content if there are links to the index.html files. I'm doing this in my site; if this happens I'll use 301 redirects or canonical.
     
    badger_, Dec 20, 2016 IP
  5. Web_Dev_Chris

    Web_Dev_Chris Well-Known Member

    Messages:
    222
    Likes Received:
    12
    Best Answers:
    1
    Trophy Points:
    105
    #5
    I think I'll have to learn PHP for Contact Forms ect...
     
    Web_Dev_Chris, Dec 20, 2016 IP
  6. ProxyDatabase

    ProxyDatabase Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    Hello @Web_Dev_Chris ,
    You can redirect all .html extensions through .htaccess with the following code:
    
    RewriteEngine On
    #redirect index.html
    RewriteCond %{THE_REQUEST} ^.*/index
    RewriteRule ^(.*)index.html /$1 [R=301,L]
    
    #hide .html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.html [NC,L]
    
    Code (markup):
    It is also recommended to redirect 404 pages like so:

    
    ErrorDocument 404 /.error/404.html
    
    Code (markup):
    You will need to create a folder under your public_html called .error or any other name you like. Within the folder create a 404.html which uses the template of your website. Now, when someone requests a page that doesn't exist, he'll be redirected to your custom 404 page.

    I will also share something interesting and probably many people will try to prove it's wrong. Maybe it is, maybe it's not.
    We have a small project that uses .html extensions and they are indexed and displayed in search engines as:
    example.com/article/example-title.html
    meanwhile :
    example.com/article/example-title responds with 404.
    We have some good ranking results on urls with .html extension at the end.
     
    ProxyDatabase, Dec 21, 2016 IP