3

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:

-example.com/home/ ----------- goes to ----- example.com/home/

-example.com/home ------------ goes to ----- example.com/home/

-example.com/home.html ------ goes to ----- example.com/home/

-example.com/home.html/ ----- goes to ----- example.com/home/

-example.com/home/.html ----- goes to ----- example.com/home/

-example.com/home/.html/ ---- goes to ----- example.com/home/

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]

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.

1 Answer 1

3

Modify the .htaccess file and insert the following

Explanation: http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/

Example:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html 

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Update the links on the pages

Then, all hyperlinks, css links, images, etc, will need to be updated to have either an absolute URL (http://www.site.com/style.css) or relative and begin with ../. Otherwise you will encounter issues such as CSS that doesn't load, links that don't work, etc.

Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately, I have tried that solution, and it does not work for me at all. I just get an error 404 not found page. The .htaccess I posted in the OP works perfectly, it just doesn't add the slash on the end of the URL.
I've updated the answer. Try this one, it's a little more clear.
That doesn't work for me either. It adds the slash, but doesn't load any CSS, or images for that matter. On top of that, when I click on any links on my site it just adds them to whatever is in the URL bar. Let's say I am on example.com/home/ and then I click test, then instead of going to example.com/test/ it goes to example.com/home/test.
Your links, CSS, images,etc, all need to be absolute or relative but beginning with "../"
Ok, I changed the .htaccess to the code you posted and it adds a slash if I type in the URL without the .html extension, but if I type it in with the .html extension it still shows it. I would like the .html extension to automatically disappear and then add a slash.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.