Menu

如何去除WordPress固定链接中的“index.php”

许多利用Windows搭建WordPress网站的用户会发现这样一个问题:在固定链接设置(wp-admin/options-permalink.php)的页面中,如果使用的是非默认固定链接,会被强制加上一个index.php。为了网站的美观和SEO优化的效果,建议大家移除index.php。

如果网站支持UrlRewrite

新建一个httpd.ini或者在原有的httpd.ini中加入:

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

保存后,上传至WordPress程序所在的根目录,即可。

如果网站不支持UrlRewrite

这种情况处理起来会比起前面一种复杂一些,还需要能够自定义404错误页面。

新建一个php文件,文件名可以任意,比如404.php。在其中加入如下的代码:

<?php
 header('HTTP/1.1 200 OK');
 $ori_qs = $_SERVER['QUERY_STRING'];
 $pattern = '/[^;]+;[^:]+:\/\/[^\/]+(\/[^\?]*)(?:\?(.*))?/i';
 preg_match($pattern, $ori_qs, $matches);
 $_SERVER['PATH_INFO'] = $matches[1] . '?' . $matches[2];
 $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
 $query_args = explode('&', $matches[2]);
 unset($_GET);
 foreach ($query_args as $arg)
 {
 $the_arg = explode('=', $arg);
 $_GET[$the_arg[0]] = $the_arg[1];
 }
 include('index.php');
 ?>

保存后,上传至WordPress程序所在的根目录,将自定义的404错误页面设置成为你保存的文件名,比如404.php

本文固定连接:https://code.zuifengyun.com/2015/01/856.html,转载须征得作者授权。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持