的.htaccess删除URL扩展,添加尾随斜线 [英] .htaccess Remove URL Extension, Add Trailing Slash

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

问题描述

我一直在试图得到这个工作,我的客户的服务器,一个网站我开发的,但我根本无法得到它的工作。基本上我试图删除HTML扩展,并添加斜线(在地址栏)。

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.

下面是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]

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

All of my files hosted on the server are in the form of FILENAME.html, and are located in the root directory.

因此​​,如果任何人能请帮助我,我真的AP preciate吧。

So, if any one could please help me out, I would really appreciate it.

推荐答案

说明: http://eisabainyo.net/weblog/2007/ 8月19日/删除文件 - 扩展 - 通过-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(<$ C C $> http://www.site.com/style.css )或相对并开始与 ../ 。否则,你会遇到的问题,如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天全站免登陆