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

时间:2012-04-01 10:21:15

标签: apache .htaccess web

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

所以,如果有人进入:

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

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

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

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

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

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

到目前为止,这是我的.htaccess,它完美地工作,并且做我想做的一切,除了在最后添加尾部斜杠。

以下是代码:

#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的形式,位于根目录中。

所以,如果有人能帮助我,我会非常感激。

1 个答案:

答案 0 :(得分:2)

修改.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,不起作用的链接等问题。

相关问题