我在
/var/www/html/
中全新安装了wordpress,我们也使用CloudFlare(我认为问题可能出在这里)。当我尝试使用带有一个子目录的默认站点时,一切正常。但只要我启用 subdir multisite define( 'MULTISITE', true );
站点就会在我尝试访问的每个页面上开始循环并显示 302 错误代码(ERR_TOO_MANY_REDIRECTS),如果我禁用它,所有内容都会恢复正常状态。
这是我的wp-config.php
:
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the installation.
* You don't have to use the website, you can copy this file to "wp-config.php"
* and fill in the values.
*
* This file contains the following configurations:
*
* * Database settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://developer.wordpress.org/advanced-administration/wordpress/wp-config/
*
* @package WordPress
*/
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'db' );
/** Database username */
define( 'DB_USER', 'user' );
/** Database password */
define( 'DB_PASSWORD', 'pass' );
/** Database hostname */
define( 'DB_HOST', 'localhost' );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
/**#@+
* Authentication unique keys and salts.
*
* Change these to different unique phrases! You can generate these using
* the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
*
* You can change these at any point in time to invalidate all existing cookies.
* This will force all users to have to log in again.
*
* @since 2.6.0
*/
define( 'AUTH_KEY', '***' );
define( 'SECURE_AUTH_KEY', '***' );
define( 'LOGGED_IN_KEY', '***' );
define( 'NONCE_KEY', '***' );
define( 'AUTH_SALT', '***' );
define( 'SECURE_AUTH_SALT', '***' );
define( 'LOGGED_IN_SALT', '***' );
define( 'NONCE_SALT', '***' );
/**#@-*/
/**
* WordPress database table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the documentation.
*
* @link https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/
*/
define( 'WP_DEBUG', true );
/* Add any custom values between this line and the "stop editing" line. */
//define('ADMIN_COOKIE_PATH', '/');
//define('COOKIE_DOMAIN', '');
//define('COOKIEPATH', '');
//define('SITECOOKIEPATH', '');
define('WP_ALLOW_MULTISITE', true);
define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', false );
define( 'DOMAIN_CURRENT_SITE', 'site.domain' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );
define('FORCE_SSL_ADMIN', true);
// in some setups HTTP_X_FORWARDED_PROTO might contain
// a comma-separated list e.g. http,https
// so check for https existence
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$_SERVER['HTTPS']='on';
define('WP_HOME', 'https://site.domain');
define('WP_SITEURL', 'https://site.domain');
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
我在使用 http 而不是 https 发送一些请求时遇到问题,所以我添加了:
define('FORCE_SSL_ADMIN', true);
// in some setups HTTP_X_FORWARDED_PROTO might contain
// a comma-separated list e.g. http,https
// so check for https existence
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$_SERVER['HTTPS']='on';
这是我的
.htaccess
:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
Options +FollowSymLinks
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress
我认为问题也可能出在我的 apache 虚拟主机配置中:
<VirtualHost *:80>
ServerName site.domain
DocumentRoot /var/www/html
RewriteEngine on
RewriteCond %{SERVER_NAME} =site.domain
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
<Directory "/var/www/html">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /var/www/html
ServerName site.domain
SSLCertificateFile /etc/letsencrypt/live/site.domain/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/site.domain/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
到目前为止,我尝试了几乎所有具有相同标题的解决方案:
define('ADMIN_COOKIE_PATH', '/'); ...
mod_rewrite
是否已启用嗨,
我希望你一切都好。我已经检查了您的设置,并且由于您使用的是 Apache WebServer,因此这里有一份解决 WordPress 多站点配置中的重定向循环问题的详细指南。
请验证 Cloudflare 的 SSL 设置并在 Cloudflare 仪表板中切换到完全(严格):
wp-config.php
wp-config.php
中存在以下配置,以实现基于子目录的多站点设置:
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false); // Subdirectory-based install
define('DOMAIN_CURRENT_SITE', 'yourdomain.com'); // Replace with your actual domain
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);
// Cloudflare Flexible SSL handling
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}
3.检查 .htaccess
多站点重写规则
.htaccess
文件包含 WordPress 基于子目录的多站点的正确重写规则:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
4.验证 WordPress Site URL
和
Home URL
siteurl
和
home
字段设置是否正确:运行此 SQL 查询来验证值:
SELECT option_name, option_value
FROM wp_options
WHERE option_name IN ('siteurl', 'home');
5.清除浏览器和 Cloudflare 缓存
mod_rewrite
模块已启用并且您的虚拟主机配置正确。
启用mod_rewrite
sudo a2enmod rewrite
sudo systemctl restart apache2
虚拟主机配置示例:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName yourdomain.com
ServerAlias www.yourdomain.com
<Directory /var/www/html>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName yourdomain.com
ServerAlias www.yourdomain.com
SSLEngine on
SSLCertificateFile /etc/ssl/certs/your_certificate.crt
SSLCertificateKeyFile /etc/ssl/private/your_key.key
<Directory /var/www/html>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
注意:请更新您自己的端口和域名来解决此问题。