且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-23 19:52:41

修改.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 无法加载、链接无效等问题.

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.