2012-04-01 154 views
2

我一直在试图让我的客户端的服务器上为我正在开发的网站工作,但我根本无法让它工作。基本上我试图删除.html扩展名,并添加一个结尾的斜杠(在URL栏中)。.htaccess删除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

修改.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)或相对并且是杜松子酒../。否则,您将遇到诸如CSS无法加载,链接不起作用等问题。

+0

不幸的是,我已经尝试过这种解决方案,它根本不适用于我。我只是得到一个错误404找不到页面。我在OP中发布的.htaccess完美地工作,它不会在URL的末尾添加斜杠。 – shootingrubber 2012-04-01 10:31:54

+0

我已经更新了答案。试试这个,它更清楚一点。 – msigman 2012-04-01 10:33:52

+0

这对我也不起作用。它添加了斜杠,但不会加载任何CSS或图像。最重要的是,当我点击我网站上的任何链接时,只需将它们添加到URL栏中的任何链接即可。假设我在example.com/home/上,然后单击测试,然后转到example.com/test/,然后转到example.com/home/test。 – shootingrubber 2012-04-01 10:36:57