Apache配置Rewrite重写后访问白屏且函数失效问题求助
问题原因
- 重写规则配置逻辑错误:当前
/test的重写规则没有添加[L]终止标记,重写完成后会继续被后面的WordPress默认重写规则拦截,最终请求还是被转发到index.php处理,而WordPress没有/test路径的对应内容,触发异常逻辑。同时你错误地把部分重写规则放在了<IfModule mod_headers.c>块内,导致规则加载逻辑混乱。 - WordPress核心未加载:你直接将请求重写到主题目录下的
page-shop.php文件,没有走WordPress的正常加载流程,文件内调用的get_header2()等WordPress原生函数不存在,PHP触发致命错误,如果你关闭了错误显示就会直接返回白屏。 - 相对路径引用失效:你代码里
include('handler/connect.php')的相对路径写法,在请求路径是/test时,PHP会从站点根目录寻找handler/connect.php,而不是从page-shop.php所在的主题目录查找,文件加载失败同样会触发报错。
解决建议
- 先调整.htaccess的规则顺序和写法,把自定义重写规则放在WordPress默认规则之前,添加
[L]标记,同时修正规则块归属:
RewriteOptions inherit Options -Multiviews +FollowSymLinks <IfModule mime_module> AddHandler application/x-httpd-ea-php73 .php .php7 .phtml </IfModule> <IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpg "access plus 1 year" ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/gif "access plus 1 year" ExpiresByType image/png "access plus 1 year" ExpiresByType text/css "access plus 1 month" ExpiresByType application/pdf "access plus 1 month" ExpiresByType text/javascript "access plus 1 month" ExpiresByType text/html "access plus 5 minutes" ExpiresByType image/x-icon "access plus 1 year" ExpiresDefault "access plus 6 hours" </IfModule> <IfModule mod_headers.c> Header set X-Endurance-Cache-Level "2" Header always set Content-Security-Policy "upgrade-insecure-requests;" #Sets Access Control Header set Access-Control-Allow-Origin 'https://mhspeakers.com/homepage' Header set Access-Control-Allow-Origin 'https://mhspeakers.com' Header set Access-Control-Allow-Origin 'origin' </IfModule> <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] # 自定义重写规则放在WP规则之前,加L标记 RewriteRule ^test$ index.php?pagename=shop [L] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
- 不要直接重写到主题PHP文件,改成重写到WordPress已有的shop页面路径,也就是上面示例里的
RewriteRule ^test$ index.php?pagename=shop [L]写法,走WordPress的正常加载流程,所有函数都会正常初始化。 - 如果一定要直接调用主题文件,需要在
page-shop.php最开头加载WordPress核心,在session_start();之前加入下面的代码:
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
- 把代码里的相对路径引用改成绝对路径,避免加载失败,比如把
include('handler/connect.php');改成:
include(__DIR__ . '/handler/connect.php');
- 临时开启PHP错误显示定位问题,在
page-shop.php最开头加入代码,排查完再关闭:
ini_set('display_errors', 1); error_reporting(E_ALL);
内容的提问来源于stack exchange,提问作者Gregory Ebbs
相关产品推荐
相关产品推荐

