Nginx给目录添加反斜杠的伪静态规则方法

talklee

温馨提示:这篇文章已超过1888天没有更新,请注意相关的内容是否还可用!

今天访问自己的博客网站发现一处问题,因为之前用过Windows和Apache的环境,并未出现此问题。

Nginx中常出现类似访问 https://www.talklee.com/info 无法打开的情况,会出现404页面,而在目录后加上一条斜杠“/”就可以访问,例如:https://www.talklee.com/info/ 呵呵,这就尴尬了~~~我知道是伪静态的原味,但是不知道具体的规则是什么......

有问题就百度啊,但是百度给我代码是错误的,请教了zbp的大神们(小峰和水水老师)然后在论坛找到的这个规则,附上源代码,其中加粗的部分就是正确的代码,不是zb程序的话,只复制加粗内容即可。

Nginx给目录添加反斜杠的伪静态规则方法 第1张

Apache .htaccess

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^([^\.]+[^/])$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

----------------------

Nginx

if (!-f $request_filename){

    rewrite ^/([^\.]+[^/])$ http://$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;

}

最新方案

  • 如果您的网站开启了https或者适用了小程序,那么请使用如下规则:

if (-d $request_filename) { 
    rewrite ^/(.*)([^/])$ https://$http_host/$1$2/ permanent; 
}

-----------------------

IIS6+ISAPI Rewrite 2.X

[ISAPI_Rewrite]

RewriteRule /(?!zb_)([^\.]+[^/]) /$1/ [RP,L]

RewriteRule /default_([0-9]+)\.html /catalog\.asp\?page=$1

RewriteRule /category/(?!zb_)(.*)_([0-9]+)/ /catalog\.asp\?cate=$1&page=$2

RewriteRule /category/(?!zb_)(.*)/ /catalog\.asp\?cate=$1

RewriteRule /author-([0-9]+)_([0-9]+).html /catalog\.asp\?auth=$1&page=$2

RewriteRule /author-([0-9]+).html /catalog\.asp\?auth=$1

RewriteRule /tags-([0-9]+)_([0-9]+).html /catalog\.asp\?tags=$1&page=$2

RewriteRule /tags-([0-9]+).html /catalog\.asp\?tags=$1

RewriteRule /post/([0-9\-]+)_([0-9]+)/ /catalog\.asp\?date=$1&page=$2

RewriteRule /post/([0-9\-]+)/ /catalog\.asp\?date=$1

RewriteRule /post/(?!zb_)(.*)/ /view\.asp\?id=$1

RewriteRule /(?!zb_)(.*)/ /view\.asp\?id=$1

--------------------------------

IIS6+ISAPI Rewrite 3.X

#ISAPI Rewrite 3

RewriteBase /

RewriteRule ^(?!zb_)([^\.]+[^/])$ /$1/ [NU,R=301]

RewriteRule ^default_([0-9]+)\.html$ /catalog.asp\?page=$1

RewriteRule ^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>

感谢大神!

文章版权声明:除非注明,否则均为李洋个人博客原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (有 12 条评论,9224人围观)
网友昵称:cjwlove
cjwlove V 游客 Google Chrome 86.0.4240.198 Windows 10 x64 7楼
2022-05-23 来自湖南 回复
感谢老李,我就知道网站有问题在你这里就能找到解决方法!!!![Fabulous]又解决了我的问题!
网友昵称:talklee
talklee V 博主 Sogou Explorer Windows 10 x64
2022-05-24 来自辽宁 回复
@cjwlove 谢谢支持哈~
网友昵称:三人行必有我师焉
三人行必有我师焉 V 游客 Google Chrome 63.0.3239.111 Samsung C7000 6楼
2019-04-12 来自上海 回复
如果是二级目录该怎么添加/
网友昵称:talklee
talklee V 博主 Sogou Explorer Windows 7 x64
2019-04-19 来自天津 回复
@三人行必有我师焉 这个没试过~
网友昵称:情书
情书 V 游客 Google Chrome 67.0.3396.99 Windows 7 x64 地板
2019-04-04 来自广东 回复
文章不错,写的很好!
网友昵称:talklee
talklee V 博主 Sogou Explorer Windows 7 x64
2019-04-08 来自上海 回复
@情书 留言很赞!
网友昵称:gfd
gfd V 游客 Google Chrome 71.0.3578.98 Windows 10 x64 凉席
2019-03-08 来自上海 回复
asfemmmmm。。看不懂怎么破?
网友昵称:talklee
talklee V 博主 Sogou Explorer Windows 7 x64
2019-03-09 来自上海 回复
@gfd 看不懂?为什么呢?
网友昵称:强大传媒
强大传媒 V 游客 Sogou Explorer Windows 7 x64 板凳
2019-01-28 来自天津 回复
学习了,很不错,微博引流www.qiangdaedu.com
网友昵称:武陵红苗
武陵红苗 V 游客 Google Chrome 49.0.2623.112 Windows 7 x64 椅子
2019-01-28 来自天津 回复
不错
网友昵称:共产主义接班人
共产主义接班人 V 游客 Google Chrome 71.0.3578.98 Windows 10 x64 沙发
2019-01-17 来自上海 回复
不错,已经用上
网友昵称:talklee
talklee V 博主 Sogou Explorer Windows 7 x64
2019-01-18 来自天津 回复
@共产主义接班人 很好用吧~

目录[+]