外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>新手入门>WordPress 伪静态规则(IIS/Apache/Nginx)

WordPress 伪静态规则(IIS/Apache/Nginx)

不少朋友总是询问 WordPress 如何添加伪静态规则,今天倡萌就总结一下 IIS/Apache/Nginx 三种环境下的伪静态规则,希望对大家有所帮助。

检测主机是否支持伪静态的方法:在WP后台 > 设置 > 固定链接,设置为 默认带?的那种结构,然后访问任何一篇文章,如果出现 404 错误,说明你的主机当前不支持 WordPress 伪静态。

IIS伪静态规则

IIS 环境是 Windows 主机常用的服务器环境,新建一个 txt 文件,将下面的代码添加到文件中:

[ISAPI_Rewrite]
# Defend your computer from some worm attacks
#RewriteRule .*(?:global.asa|default\.ida|root\.exe|\.\.).* . [F,I,O]
# 3600 = 1 hour

CacheClockRate 3600
RepeatLimit 32
 
# Protect httpd.ini and httpd.parse.errors files
# from accessing through HTTP
# Rules to ensure that normal content gets through

RewriteRule /tag/(.*) /index\.php\?tag=$1
RewriteRule /software-files/(.*) /software-files/$1 [L]
RewriteRule /images/(.*) /images/$1 [L]
RewriteRule /sitemap.xml /sitemap.xml [L]
RewriteRule /favicon.ico /favicon.ico [L]
# For file-based wordpress content (i.e. theme), admin, etc.
RewriteRule /wp-(.*) /wp-$1 [L]
# For normal wordpress content, via index.php
RewriteRule ^/$ /index.php [L]
RewriteRule /(.*) /index.php/$1 [L]

然后另存为 httpd.ini 文件,上传到WordPress站点的根目录即可。

Apache伪静态规则

Apache是 Linux 主机下常见的环境,现在一般的 Linux 虚拟主机都采用这种环境。新建一个 htaccess.txt 文件,添加下面的代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

然后上传到 WordPress 站点的根目录,重命名为 .htaccess 即可

Nginx伪静态规则

Nginx环境一般是Linux 主机 VPS或服务器用户用的比较多,这些用户一般都会自己配置Nginx,或者有专门的人帮你配置,打开 nginx.conf 或者某个站点的配置环境,比如 wpdaxue.com.conf(不同人配置的不一样),在  server   { } 大括号里面添加下面的代码:

location / {
	try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

保存,重启 Nginx 即可。

注:如果以上规则不适用,请自己查看WP官方文档:https://codex.wordpress.org/Nginx

题外话:倡萌一直不推荐在 windows 的IIS服务器下安装 WordPress,因为 IIS 环境运行php程序的效率,相对同等配置下 Linux 的 Apache 和 Nginx 环境,要低的多,具体可以看看https://www.wpdaxue.com/wordpress-hosting.html

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
新手入门

如何查看WordPress分类目录、标签和文章的ID

2013-9-1 10:13:41

新手入门

编辑WordPress主题的 functions.php 文件的注意事项

2013-12-3 9:13:30

20 条回复 A文章作者 M管理员
  1. 苏乐乐

    不会配置08影院的伪静态 有人会的么?

  2. tianxinyuran

    我的也是用的便宜的WIN主机,静态化,后面必须有index.php,看着百度的教程弄什么404也没弄好,最后自己没辙了,挨个的找的源文件修改的:
    步骤:1.固定链接选择第一个,默认的 ?p=123这样的
    2.更改.htaccess RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]

    RewriteRule ^cat-([^/]+).html ?cat=$1
    RewriteRule ^page_id-([^/]+).html ?page_id=$1
    RewriteRule ^p-([^/]+).html ?p=$1
    楼主的上面的那个第7行,一定不要有,那个会把自己写的规则导入到首页··就是RewriteRule . /index.php [L] 这个不要;
    3.在wp-includes 》link-template.php 这个文件下搜索”?p=”,”?page_id=”就是改成默认固定链接之后,前台文章基本页面,以及page页面的链接;找到之后改成“p-””page_id-” ,在加个“.html”;如:$permalink = home_url(‘p-‘ . $post->ID.’.html’);本来里面的”.html”是没有的,自己加上$link = home_url( ‘page_id-‘ . $post->ID.’.html’ );这是另外的一个。
    4.?cat= 分类的链接是这个的,但是在上面的3中找不到,后来想的办法是把导航的链接进行更改,文件是:wp-includes>nav-menu-template.php 这个文件里面,需要自己写一段PHP代码。首先找到:
    $value = ( ‘href’ === $attr ) ? esc_url( $value ) : esc_attr( $value );
    $attributes .= ‘ ‘ . $attr . ‘=”‘ . $value . ‘”‘;
    这两行代码;然后在他们中见插入一个if判断来处理链接字符
    if($value!=’/’){
    $subV=str_replace(“?”,””,$value);
    $reV=str_replace(“=”,”-“,$subV);
    $value=$reV.”.html”;

    }
    更改后是:$value = ( ‘href’ === $attr ) ? esc_url( $value ) : esc_attr( $value );
    if($value!=’/’){
    $subV=str_replace(“?”,””,$value);
    $reV=str_replace(“=”,”-“,$subV);
    $value=$reV.”.html”;

    }
    $attributes .= ‘ ‘ . $attr . ‘=”‘ . $value . ‘”‘;
    4.通过上述方法页面上的链接都会以html展现
    5.但是在文章列表页面会出现翻页无法翻页的情况,原因是翻页只能用动态的进行搜索?cat=2 ,这样的···wordpress的翻页是直接获取当前的列表的url然后在那个基础上进行查询。建议如果有时间可以自己写个翻页的代码,或者按照如下方法·····(我弄了一天的时间···这几个问题···醉了)
    首先,我用的是wp-pagenavi 这个插件弄的翻页··原始的太丑···;通过FTP找到插件的目录:wp-content/plugins/wp-pagenavi/core.php 这个里面的269行:$attr[‘href’]= $this->get_url( $page );这行,改成
    $attr[‘href’] = “javascript:void(0);”;
    $attr[‘data-href’] = $this->get_url( $page );
    $attr[‘onclick’] = “navPage(this)”;
    第一行是把原来的href弄成空链接,这样前台的翻页按钮就不会跳转页面;
    第二行是把url写入data-href 自定义属性··用来存储url
    第三行,添加··onclick方法里面的函数是我自己写的,下面会提到;
    5.然后找到footer.php,写入js . 这个文件在主题文件夹里面,当然你可以随意找个公共页面,或者写在归档archive.php这个里面;JS如下:
    function navPage(e){
    var httpV=e.getAttributeNode(“data-href”).value;
    var patt=/paged/;
    var result=patt.test(httpV);
    if(result==true){
    console.log(httpV);
    var n=httpV.replace(“cat-“,”?cat=”);
    n=n.replace(“.html?”,”&”);
    window.location.replace(n);
    }else{
    e.getAttributeNode(“href”).value=httpV;
    }
    }
    这个我就不解释了,就是把翻页的a里面的date-href数据拿出来然后改成动态的,然后加载页面,完事判断在动态链接的情况下直接读取data-href插入到href进行跳转··以上做个总结

  3. boyboai

    菜鸟学习一下

  4. 求大学解答,我的主机是win,用httpd.ini和htaccess设置了伪静态,去掉了固定连接中的index.php,但是点击回复某个评论时,链接里又出现了index.php,导致指向404.

  5. LZ,问下两个问题:
    1、我的nginx.conf文件没有server {},自己手动添加后用 nginx -t提示语法错误。
    2、service httpd start命令不能重启nginx服务,提示如下问题。
    Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName
    (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
    no listening sockets available, shutting down
    Unable to open logs

  6. nginx 的版本有问题,

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索