首页 未命名内容详情

ZBlog系统强制网站目录链接统一加斜杠访问

2026-01-10 2 897698882
ZBlog系统强制网站目录链接统一加斜杠访问

站点名称:ZBlog系统强制网站目录链接统一加斜杠访问

所属分类:未命名

官方网址:

SEO查询: 爱站网 站长工具

进入网站

站点介绍

通常情况下,url在末尾处是否加上斜杠“/”,对用户访问是没有太大影响的,大多数的Web 服务器均能正确解析,在传统意义上说,url末尾是没有反斜杠的,有没有反斜杠的意义在于该 url 是指向一个文件还是一个目录。

举个例子:

https://www.zblogmb.com/theme

https://www.zblogmb.com/theme/

url末尾带斜杠 / 的是指向目录/theme/,不带的是文件/theme,是两个不同的地址,一般来说,索引页面(如文章列表或分类)作为网站目录,而内容页面作为文件对于服务器来说,如果访问目录,则会根据规则访问该目录下的默认文件(index.html、index.htm、index.php之类)。

如:访问/theme/服务器会寻找/theme/index.html文件,而访问/theme服务器则会寻找/theme文件,如果没有查询到该目录下的文件时,有可能导致web服务器不能正确解析而出现返回404 错误,这时就要对链接进行定向处理让目录统一加上斜杠访问。

网站目录链接统一加斜杠的方法:

Apache .htaccess

<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^([^\.]+[^/])$ /$1/ [L,R=301]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule>

IIS6+ISAPI Rewrite 2.X

[ISAPI_Rewrite]RewriteRule /(?!zb_)([^\.]+[^/]) /$1/ [RP,L]RewriteRule /default_([0-9]+)\.html /catalog\.asp\?page=$1RewriteRule /category/(?!zb_)(.*)_([0-9]+)/ /catalog\.asp\?cate=$1&page=$2RewriteRule /category/(?!zb_)(.*)/ /catalog\.asp\?cate=$1RewriteRule /author-([0-9]+)_([0-9]+).html /catalog\.asp\?auth=$1&page=$2RewriteRule /author-([0-9]+).html /catalog\.asp\?auth=$1RewriteRule /tags-([0-9]+)_([0-9]+).html /catalog\.asp\?tags=$1&page=$2RewriteRule /tags-([0-9]+).html /catalog\.asp\?tags=$1RewriteRule /post/([0-9\-]+)_([0-9]+)/ /catalog\.asp\?date=$1&page=$2RewriteRule /post/([0-9\-]+)/ /catalog\.asp\?date=$1RewriteRule /post/(?!zb_)(.*)/ /view\.asp\?id=$1RewriteRule /(?!zb_)(.*)/ /view\.asp\?id=$1

IIS6+ISAPI Rewrite 3.X

#ISAPI Rewrite 3RewriteBase /RewriteRule ^(?!zb_)([^\.]+[^/])$ /$1/ [NU,R=301]RewriteRule ^default_([0-9]+)\.html$ /catalog.asp\?page=$1RewriteRule ^category/(?!zb_)(.*)_([0-9]+)/$ /catalog.asp\?cate=$1&page=$2 [NU]RewriteRule ^category/(?!zb_)(.*)/$ /catalog.asp\?cate=$1 [NU]RewriteRule ^author-([0-9]+)_([0-9]+).html$ /catalog.asp\?auth=$1&page=$2 [NU]RewriteRule ^author-([0-9]+).html$ /catalog.asp\?auth=$1 [NU]RewriteRule ^tags-([0-9]+)_([0-9]+).html$ /catalog.asp\?tags=$1&page=$2 [NU]RewriteRule ^tags-([0-9]+).html$ /catalog.asp\?tags=$1 [NU]RewriteRule ^post/([0-9\-]+)_([0-9]+)/$ /catalog.asp\?date=$1&page=$2 [NU]RewriteRule ^post/([0-9\-]+)/$ /catalog.asp\?date=$1 [NU]RewriteRule ^post/(?!zb_)(.*)/$ /view.asp\?id=$1 [NU]RewriteRule ^(?!zb_)(.*)/$ /view.asp\?id=$1 [NU]

IIS7、7.5、8+Url Rewrite

<?xml version="1.0" encoding="UTF-8"?><configuration><system.webServer> <rewrite>  <rules>    <rule name="//" stopProcessing="true">     <match url="^(?!zb_)[^\.]+[^/]$"/>     <action type="Redirect" redirectType="Permanent" url="{R:0}/"/>    </rule>    <rule name="Imported Rule Default+Page" stopProcessing="true">     <match url="^default_([0-9]+)\.html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?page={R:1}" />    </rule>    <rule name="Imported Rule Category+Page" stopProcessing="true">     <match url="^category-([0-9]+)_([0-9]+).html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?cate={R:1}&page={R:2}" />    </rule>    <rule name="Imported Rule Category" stopProcessing="true">     <match url="^category-([0-9]+).html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?cate={R:1}" />    </rule>    <rule name="Imported Rule Author+Page" stopProcessing="true">     <match url="^author-([0-9]+)_([0-9]+).html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?auth={R:1}&page={R:2}" />    </rule>    <rule name="Imported Rule Author" stopProcessing="true">     <match url="^author-([0-9]+).html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?auth={R:1}" />    </rule>    <rule name="Imported Rule Tags+Page" stopProcessing="true">     <match url="^tags-([0-9]+)_([0-9]+).html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?tags={R:1}&page={R:2}" />    </rule>    <rule name="Imported Rule Tags" stopProcessing="true">     <match url="^tags-([0-9]+).html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?tags={R:1}" />    </rule>    <rule name="Imported Rule Date+Page" stopProcessing="true">     <match url="^date-([0-9\-]+)_([0-9]+).html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?date={R:1}&page={R:2}" />    </rule>    <rule name="Imported Rule Date" stopProcessing="true">     <match url="^date-([0-9\-]+).html$" ignoreCase="false" />     <action type="Rewrite" url="catalog.asp?date={R:1}" />    </rule>    <rule name="Imported Rule Article" stopProcessing="true">     <match url="^post/(?!zb_)(.*).html$" ignoreCase="false" />     <action type="Rewrite" url="view.asp?id={R:1}" />    </rule>    <rule name="Imported Rule Page" stopProcessing="true">     <match url="^(?!zb_)(.*).html$" ignoreCase="false" />     <action type="Rewrite" url="view.asp?id={R:1}" />    </rule>  </rules> </rewrite></system.webServer></configuration>

Nginx

if (!-f $request_filename){   rewrite ^/([^\.]+[^/])$ $scheme://$host/$1$2/ permanent;}if (-f $request_filename/index.html){   rewrite (.*) $1/index.html break;}if (-f $request_filename/index.php){   rewrite (.*) $1/index.php;}if (!-f $request_filename){   rewrite (.*) /index.php;}

在规则中加上以上代码即可给目录自动加上斜杠访问,防止出现404错误问题。