繁体   English   中英

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

[英].htaccess Remove URL Extension, Add Trailing Slash

我一直试图让我的客户端服务器上的这个工作用于我正在开发的网站,但我根本无法让它工作。 基本上我试图删除.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的形式,位于根目录中。

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

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

暂无
暂无

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

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