本文作者:DurkBlue

Nginx给目录添加反斜杠的伪静态规则步骤推荐

DurkBlue2020-01-071797
Nginx给目录添加反斜杠的伪静态规则步骤摘要: 今天访问自己的博客网站发现一处问题,因为之前用过Windows和Apache的环境,并未出现此问题。Nginx中常出现类似访问 https://hepuhua.cn/info&nb...

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

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

有问题就百度啊,但是百度给我代码是错误的,请教了一些大神们(小峰和水水老师)然后在论坛找到的这个规则,附上源代码,其中加粗的部分就是正确的代码。

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;

}

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

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>

感谢大神!


此篇文章由DurkBlue博客申请发布,转载吧请注明来处
文章投稿或转载声明

来源:DurkBlue版权归原作者所有,转载请保留出处。本站文章发布于 2020-01-07
温馨提示:文章内容系作者个人观点,不代表DurkBlue博客对其观点赞同或支持。

赞(0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享