简体   繁体   中英

.htaccess Remove URL Extension, Add Trailing Slash

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM