dede web.config 伪静态规则怎么写?

99ANYc3cd6
预计阅读时长 36 分钟
位置: 首页 DEDE建站 正文
  1. 优化SEO:URL看起来像静态网页(如 http://www.yoursite.com/abc.html),更容易被搜索引擎抓取和收录。
  2. 提升用户体验:URL更简洁、友好,用户更容易理解和记忆。
  3. 增强安全性:隐藏了动态脚本(如 .php),在一定程度上可以防止一些常见的攻击。

核心概念

  • .htaccess 文件:这是 Apache 服务器使用的配置文件,如果你的网站服务器是 Apache,就需要使用这个。
  • web.config 文件:这是 IIS 服务器(Windows Server)使用的配置文件,如果你的网站服务器是 IIS 7.0 及以上版本,就需要使用这个。

重要提示:在操作之前,请务必备份你的网站文件和数据库,以防配置错误导致网站无法访问。

dede web.config 伪静态
(图片来源网络,侵删)

第一步:开启 DedeCMS 伪静态功能

在配置服务器文件之前,必须先在 DedeCMS 后台开启伪静态开关。

  1. 登录你的 DedeCMS 后台。
  2. 进入 “系统” -> “系统基本参数”
  3. 在左侧菜单中找到 “核心设置”
  4. 找到 “是否使用伪静态” 这个选项,将其值修改为 “是”
  5. 点击页面底部的 “保存” 按钮。

完成这一步后,DedeCMS 在生成链接时就会尝试使用伪静态的格式(如 /archives/123/456.html)。


第二步:根据你的服务器选择并配置文件

请根据你的服务器环境,选择以下对应的操作指南。

Apache 服务器(使用 .htaccess 文件)

这是最常见的情况,尤其是在 Linux 主机或虚拟主机上。

dede web.config 伪静态
(图片来源网络,侵删)

操作步骤:

  1. 在你的网站根目录(public_htmlwww)下,找到或创建一个名为 .htaccess 的文件。

    • 如果文件不存在,新建一个空白的文本文档,然后将其重命名为 .htaccess(注意,文件名前面有一个点,没有后缀名)。
    • 注意:在 Windows 资源管理器中,默认是看不到隐藏文件和扩展名的,你可能需要在文件夹选项中设置显示。
  2. 将以下代码完整复制粘贴.htaccess 文件中。

# DedeCMS 伪静态规则 (Apache)
# 如果你的网站放在子目录,请将 RewriteBase / 修改为 RewriteBase /你的子目录/
<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On
  RewriteBase /
  # 首页
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^index\.html$ index.php [L]
  # 列表页
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)/index\.html$ $index.php [L]
  RewriteRule ^(.*)/list([0-9]+)\.html$ $index.php?action=list&tid=$2 [L]
  # 文章页
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)/view([0-9]+)\.html$ $index.php?action=view&arcid=$2 [L]
  # 自定义页面(适用于单页、联系我们等)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)/([a-z]+)\.html$ $index.php?action=$2 [L]
  # 会员中心
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^member/([a-z]+)\.html$ $member/index.php?$1 [L]
  RewriteRule ^member/([a-z]+)/([a-z]+)\.html$ $member/$2.php?$1 [L]
  # 问答频道
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^question/([a-z]+)\.html$ $question/index.php?$1 [L]
  RewriteRule ^question/([a-z]+)/([a-z]+)\.html$ $question/$2.php?$1 [L]
  # 小频道
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^group/([a-z]+)\.html$ $group/index.php?$1 [L]
  RewriteRule ^group/([a-z]+)/([a-z]+)\.html$ $group/$2.php?$1 [L]
  # 图片集
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^special/([a-z]+)\.html$ $special/index.php?$1 [L]
  RewriteRule ^special/([a-z]+)/([a-z]+)\.html$ $special/$2.php?$1 [L]
  # 自定义表单
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^([a-z]+)\.php$ $index.php?action=$1 [L]
</IfModule>
  1. 保存文件,并将其通过 FTP 或文件管理器上传到网站根目录。

    dede web.config 伪静态
    (图片来源网络,侵删)
  2. 检查权限:确保 .htaccess 文件的权限设置为 644,如果不行,可以尝试 755

  3. 重启 Apache(如果需要):有时修改 .htaccess 后需要重启 Apache 服务才能生效,具体操作取决于你的主机控制面板。


IIS 7.0+ 服务器(使用 web.config 文件)

如果你的网站托管在 Windows Server 的 IIS 7.0 或更高版本上,你需要使用 web.config 文件。

操作步骤:

  1. 在你的网站根目录下,找到或创建一个名为 web.config 的文件,如果不存在,请创建一个。

  2. 将以下代码完整复制粘贴web.config 文件中。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <!-- 首页 -->
                <rule name="homepage" stopProcessing="true">
                    <match url="^index\.html$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
                <!-- 列表页 -->
                <rule name="list" stopProcessing="true">
                    <match url="^(.*)/index\.html$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
                <rule name="list2" stopProcessing="true">
                    <match url="^(.*)/list([0-9]+)\.html$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?action=list&amp;tid={R:2}" />
                </rule>
                <!-- 文章页 -->
                <rule name="view" stopProcessing="true">
                    <match url="^(.*)/view([0-9]+)\.html$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?action=view&amp;arcid={R:2}" />
                </rule>
                <!-- 自定义页面 -->
                <rule name="page" stopProcessing="true">
                    <match url="^(.*)/([a-z]+)\.html$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?action={R:2}" />
                </rule>
                <!-- 会员中心 -->
                <rule name="member" stopProcessing="true">
                    <match url="^member/([a-z]+)\.html$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="member/index.php?{R:1}" />
                </rule>
                <rule name="member2" stopProcessing="true">
                    <match url="^member/([a-z]+)/([a-z]+)\.html$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="member/{R:2}.php?{R:1}" />
                </rule>
                <!-- 其他频道(问答、小频道等) -->
                <!-- ... 可以根据需要添加更多规则,与会员中心类似 ... -->
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
  1. 保存文件,并将其上传到网站根目录。

  2. 检查 URL 重写模块web.config 的功能依赖于 IIS 的 URL Rewrite 模块,如果你的服务器上没有安装这个模块,配置将不会生效,大多数 Windows 主机服务商默认已安装,如果没有,你需要联系你的主机商或自行安装。


第三步:验证和测试

  1. 清空缓存:登录 DedeCMS 后台,进入 “系统” -> “系统基本参数” -> “性能选项”,点击 “清空所有缓存”
  2. 更新文章:修改一篇文章,重新生成一下,看看文章链接是否变成了 .html 格式。
  3. 访问测试
    • 访问你的首页,看看是否正常。
    • 访问一个列表页面(如 你的域名/列表/1.html)。
    • 访问一篇文章的页面(如 你的域名/archives/123/456.html)。
    • 如果都能正常打开,说明伪静态配置成功!

常见问题排查

  • 网站打不开,显示 500 或 404 错误

    • 检查语法.htaccessweb.config 文件的语法是否有错误,特别是 XML 文件,一个标签的闭合错误就会导致整个文件无法解析。
    • 检查文件位置:确保文件放在了正确的网站根目录。
    • 检查权限:确保文件权限设置正确。
    • 临时删除:如果无法解决,可以暂时将文件重命名(如 .htaccess.bak),看看网站是否能恢复,以确定问题是否出在这个文件上。
  • 伪静态规则不生效

    • 确认服务器环境:确保你为正确的服务器(Apache 或 IIS)配置了正确的文件。
    • 确认模块:对于 IIS,确认 URL Rewrite 模块已安装并启用,对于 Apache,确认 mod_rewrite 模块已启用(LoadModule rewrite_module modules/mod_rewrite.so 这行在 httpd.conf 中没有被注释掉)。
    • 联系主机商:如果自己无法解决,最好的方法是联系你的主机服务商,他们通常能帮你快速定位问题。

希望这份详细的指南能帮助你成功为 DedeCMS 配置好伪静态!

-- 展开阅读全文 --
头像
织梦dedecms资讯模板如何快速搭建文章类网站?
« 上一篇 12-05
织梦CMS list标签如何调用新增字段?
下一篇 » 12-05

相关文章

取消
微信二维码
支付宝二维码

目录[+]