.htaccess 删除 URL 扩展名,添加尾部斜杠 [英] .htaccess Remove URL Extension, Add Trailing Slash

查看:36
本文介绍了.htaccess 删除 URL 扩展名,添加尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让它在我正在开发的网站的客户端服务器上工作,但我根本无法让它工作.基本上我试图删除 .html 扩展名,并添加一个尾部斜杠(在 URL 栏中).

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

所以如果有人进入:

-example.com/home/----------- 转到 ----- example.com/home/

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

-example.com/home ------------ 转到 ----- example.com/home/

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

-example.com/home.html ------ 转到 ----- example.com/home/

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

-example.com/home.html/----- 转到 ----- example.com/home/

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

-example.com/home/.html ----- 转到 ----- example.com/home/

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

-example.com/home/.html/---- 转到 ----- example.com/home/

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

到目前为止,这是我的 .htaccess 文件,它运行良好,可以完成我想做的所有事情,除了在末尾添加斜杠.

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

代码如下:

#force www
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*) http://www.%{HTTP_HOST}/1 [R=301,L]

# remove .html ONLY if requested directly
RewriteCond %{THE_REQUEST} (.htmlsHTTP/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]

我在服务器上托管的所有文件都是 FILENAME.html 的形式,并且位于根目录中.

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.

推荐答案

修改.htaccess文件并插入以下内容

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

示例:

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]

更新页面上的链接

然后,所有超链接、css 链接、图像等都需要更新为绝对 URL (http://www.site.com/style.css) 或相对 URL并以 ../ 开头.否则会遇到 CSS 无法加载、链接无效等问题.

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.

这篇关于.htaccess 删除 URL 扩展名,添加尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆