.htaccess 删除 URL 扩展名,添加尾部斜杠

编程入门 行业动态 更新时间:2024-10-15 02:26:27
本文介绍了.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/home/----------- 转到 ----- example/home/

-example/home/ ----------- goes to ----- example/home/

-example/home ------------ 转到 ----- example/home/

-example/home ------------ goes to ----- example/home/

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

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

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

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

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

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

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

-example/home/.html/ ---- goes to ----- example/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 ^(.*) 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文件并插入以下内容

说明:eisabainyo/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 (www.site/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 (www.site/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 扩展名,添加尾部斜杠

本文发布于:2023-11-01 13:14:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   扩展名   尾部   htaccess   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!