Typecho 的伪静态设置

Typecho 是一个基于 PHP 的简洁的开源博客程序。它使用多种数据库储存数据,在 GNU GPLv2 许可证下发行。相对于 WordPress 而言,更加简洁,原生支持 Markdown,非常易于写文章。

对于 Typecho 开启伪静态一般需要两步,一是在 Typecho 后台进行设置,使用地址重写功能,如果出现提示,点击启用。仅仅在 Typecho 后台设置永久链接是不够的,还需要服务器进行相关配置进行配合,即利用服务器的 rewrite 功能来隐藏域名后的index.php。

提示:如果在 Typecho 后台启用地址重写功能出现红色提示重写功能检测失败,请检查你的服务器设置,选择仍然启用此功能,勾选即可

Linux Nginx 环境

Nginx 通过修改 nginx.conf 来实现功能控制,只需要在 server 模块里添加以下代码即可:

 

if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php$1 last;
}

如果使用宝塔安装 Nginx 环境,只需在网站设置的伪静态里面填入上述代码即可,或者选择宝塔自带的 typecho 伪静态,下拉选择框即可

Linux Apache 环境

修改 Apache 的配置文件 httpd.conf 使其支持 mod_rewrite 模块:注释掉#LoadModule rewrite_module modules/mod_rewrite.so前面的#号,没有则添加,但必须独占一行;同时,把文件中 AllowOverride None 换成 AllowOverride All 使 Apache 支持 .htaccess 文件

  • 虚拟主机不需要进行上面这个操作,如果面板里有开启伪静态支持,则需要开启
  • 通过 CentOS 下 yum 安装的 Apache,默认的配置文件为:/etc/httpd/conf/httpd.conf;
  • 通过 Debian/Ubuntu 下 apt 安装的 Apache,默认的配置文件为:/etc/apache2/apache2.conf 或者 /etc/apache2/httpd.conf;

在网站根目录下的.htaccess文件中添加代码,如没有该文件,则先创建:

 

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]

</IfModule>

也可以把上述代码直接放到网站配置文件的VirtualHost下面,但不建议这样操作

Windows IIS 环境

Windows 下一般是通过修改根目录下的 httpd.ini 文件或者 web.config 文件实现。

修改 httpd.ini 文件

 

[ISAPI_Rewrite]
RewriteRule /(.*).html /index.php/$1.html [L]

也可以设置的更详细些:

 

[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
# 中文tag解决
RewriteRule /tag/(.*) /index\.php\tag=$1
# sitemapxml
RewriteRule /sitemap.xml /sitemap.xml [L]
RewriteRule /favicon.ico /favicon.ico [L]
# 内容页
RewriteRule /(.*).html /index.php/$1.html [L]
# 评论
RewriteRule /(.*)/comment /index.php/$1/comment [L]
# 分类页
RewriteRule /category/(.*) /index.php/category/$1 [L]
# 分页
RewriteRule /page/(.*) /index.php/page/$1 [L]
# 搜索页
RewriteRule /search/(.*) /index.php/search/$1 [L]
# feed
RewriteRule /feed/(.*) /index.php/feed/$1 [L]
# 日期归档
RewriteRule /2(.*) /index.php/2$1 [L]
# 上传图片等
RewriteRule /action(.*) /index.php/action$1 [L]

修改 web.config

 

<?xml version="1.0" encoding="UTF-8"?>
<!--web.config url rewrite-->
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="typecho" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}"/>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

至于究竟是用 httpd.ini,还是 web.config,一般优先尝试 web.config,或者看看目录下已经有的是哪一个。。

 

阅读剩余
THE END