我一直在试图让我的客户端的服务器上为我正在开发的网站工作,但我根本无法让它工作。基本上我试图删除.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的形式,并且都位于根目录下。
所以,如果有人能帮助我,我会非常感激。
不幸的是,我已经尝试过这种解决方案,它根本不适用于我。我只是得到一个错误404找不到页面。我在OP中发布的.htaccess完美地工作,它不会在URL的末尾添加斜杠。 – shootingrubber 2012-04-01 10:31:54
我已经更新了答案。试试这个,它更清楚一点。 – msigman 2012-04-01 10:33:52
这对我也不起作用。它添加了斜杠,但不会加载任何CSS或图像。最重要的是,当我点击我网站上的任何链接时,只需将它们添加到URL栏中的任何链接即可。假设我在example.com/home/上,然后单击测试,然后转到example.com/test/,然后转到example.com/home/test。 – shootingrubber 2012-04-01 10:36:57