.htaccess Remove URL Extension, Add Trailing Slash

Discussion in 'Apache' started by shootingrubber, Mar 31, 2012.

  1. #1
    I have been trying to get this to work on my client's server for a website I am developing, but I simply cannot get it to work. Basically I am trying to remove the .html extensions, and add a trailing slash (in the URL bar).

    So if someone enters:

    [TABLE="class: grid, width: 700, align: left"]
    [TR]
    [TD]example.com/home/[/TD]
    [TD]goes to[/TD]
    [TD]example.com/home/[/TD]
    [/TR]
    [TR]
    [TD]example.com/home[/TD]
    [TD]goes to[/TD]
    [TD]example.com/home/[/TD]
    [/TR]
    [TR]
    [TD]example.com/home.html[/TD]
    [TD]goes to[/TD]
    [TD]example.com/home/[/TD]
    [/TR]
    [TR]
    [TD]example.com/home.html/[/TD]
    [TD]goes to[/TD]
    [TD]example.com/home/[/TD]
    [/TR]
    [/TABLE]

    Here is my .htaccess so far, which works PERFECTLY, and does everything I want it do, except add the trailing slash at the end.

    Here is that code:
    #force www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    
    # remove .html ONLY if requested directly
    RewriteCond %{THE_REQUEST} (\.html\sHTTP/1)
    RewriteRule ^(.+)\.html$ /$1 [R=301,L,QSA]
    
    
    # remove trailing slash ONLY if it is not an existing folder
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    
    # rewrite to FILENAME.html if such file does exist and is not a folder
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ /$1.html [L,QSA]
    Code (markup):
    All of my files hosted on the server are in the form of FILENAME.html, and are located in the root directory.

    So, if any one could please help me out, I would really appreciate it.
     
    shootingrubber, Mar 31, 2012 IP
  2. RoscoeT

    RoscoeT Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    RoscoeT, Apr 11, 2012 IP