[英]WordPress site not connecting to DB on non-www
I'm having an issue with my WordPress site where it's unable to connect to the database on the non-www domain. 我的WordPress网站出现问题,无法连接到非www域上的数据库。 I'm trying to rewrite all non-www requests as www with the following rule, but it's had no effect:
我正在尝试使用以下规则将所有非www请求重写为www,但这没有任何效果:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
On my server, I currently have two .htaccess
files–one in the root directory, which redirects the naked domain into a sub-directory that contains my website (see below), and another in this sub-directory, which rewrites some default WordPress URLs. 在我的服务器上,我目前有两个
.htaccess
文件-一个在根目录中,该文件将裸域重定向到一个包含我的网站的子目录(请参见下文),另一个在此子目录中,该目录重写了一些默认的WordPress网址。
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/sub-directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub-directory/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ sub-directory/index.php [L]
</IfModule>
For DNS, I have an A record that points @
to my server's IP address, and a CNAME that points www
to @
. 对于DNS,我有一个A记录,该记录将
@
指向我的服务器的IP地址,并将一个CNAME将该www
指向@
。
My wp-config.php
file includes the following condition for reading database credentials: 我的
wp-config.php
文件包含以下读取数据库凭据的条件:
if($_SERVER['SERVER_NAME'] !== 'www.domain.com') {
include(dirname(__FILE__) . '/wp-config-local.php');
define('WP_LOCAL', true);
}else {
// Production credentials
}
I understand that domain.com fails to connect to my database because its $_SERVER['SERVER_NAME']
becomes domain.com
instead of www.domain.com
, but instead of adding both in the credentials condition, I thought the .htaccess
rewrite would get around this. 我知道domain.com无法连接到我的数据库,因为它的
$_SERVER['SERVER_NAME']
变成了domain.com
而不是www.domain.com
,但是我没有将两者都添加到凭据条件中,而是认为.htaccess
重写会避开这个。 I'm struggling to determine where exactly the issue is. 我正在努力确定问题出在哪里。 Thanks in advance
提前致谢
The issue was that the non-www rule was executed after the sub-directory redirect. 问题是在子目录重定向后执行了非www规则。 Once placed before, everything worked as expected.
一旦放置在前面,一切都会按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.