Wordpress .htaccess www 重定向失败

Wordpress .htaccess www redirect fail

我刚刚被要求帮助管理一个慈善 WordPress 网站。它使用的是 http,我想将 .htaccess 重定向修改为 www。 .htaccess 文件看起来很标准,在开头添加了 404:

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

我尝试了几次添加 www 重定向代码,但每次都失败:

ErrorDocument 404 /error.php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^l-a-m.org [NC]
RewriteRule ^(.*)$ http://www.l-a-m.org/ [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

我也试过将重定向块从 # WordPress 代码中移出并将其放在文件的开头,但这也不起作用。

我是 WordPress 的新手所以任何人都可以帮助并告诉我哪里出错了吗?

尝试替换:

RewriteCond %{HTTP_HOST} ^l-a-m.org [NC]
RewriteRule ^(.*)$ http://www.l-a-m.org/ [L,R=301]

与:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]

您还提到了之前的尝试 "keep failing"。你能告诉我们出了什么问题吗,因为它可能也会导致上述方法失败。

你可以试试:

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/ [QSA,L,R=301]

或强制 https :

RewriteCond %{HTTP_HOST} ^example.com$
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://www.example.com/ [QSA,L,R=301]

example.com 替换为您想要的主机。

编辑重要:

在 WordPress 中,不要使用 .htaccess,使用 wp-config.php 并使用正确的主机名调整两行:

define('WP_HOME','http://www.domainname.com');
define('WP_SITEURL','http://www.domainname.com');

Shim-Sao 在他的评论中的回答...修改 .htaccess 文件没有用,所以我编辑了 wp-config.php 并添加了以下有效的行:

define('WP_HOME','http://www.l-a-m.org');
define('WP_SITEURL','http://www.l-a-m.org');

/* That's all, stop editing! Happy blogging. */