简体   繁体   English

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

[英].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). 基本上我试图删除.html扩展名,并添加一个尾部斜杠(在URL栏中)。

So if someone enters: 所以如果有人进入:

-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 ------------转到----- 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/ -example.com/home/.html/ ----转到----- 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. 到目前为止,这是我的.htaccess,它完美地工作,并且做我想做的一切,除了在最后添加尾部斜杠。

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. 托管在服务器上的所有文件都采用FILENAME.html的形式,位于根目录中。

So, if any one could please help me out, I would really appreciate it. 所以,如果有人能帮助我,我会非常感激。

Modify the .htaccess file and insert the following 修改.htaccess文件并插入以下内容

Explanation: http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/ 说明: 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 ../ . 然后,所有超链接,css链接,图像等都需要更新为具有绝对URL( http://www.site.com/style.css )或亲戚,并以../开头。 Otherwise you will encounter issues such as CSS that doesn't load, links that don't work, etc. 否则,您将遇到诸如无法加载的CSS,不起作用的链接等问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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