简体   繁体   English

Wordpress .htaccess www重定向失败

[英]Wordpress .htaccess www redirect fail

I've just been asked to help administer a charities WordPress site. 我刚刚被要求帮助管理慈善WordPress网站。 It's using http and I'd like to modify the .htaccess redirect to www. 它使用的是http,我想将.htaccess重定向修改为www。 The .htaccess file looks bog standard, with the addition of a 404 at the start: .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

I've tried a couple of times to add the www redirect code but it keeps failing every time: 我已经尝试过几次添加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/$1 [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

I've also tried moving the redirect block out of the # WordPress code and putting it at the start of the file, but this doesn't work either. 我也尝试过将重定向块移出#WordPress代码,并将其放在文件的开头,但这也不起作用。

I'm a complete newbie to WordPress so can anyone help and tell me where I'm going wrong please? 我是WordPress的新手,所以有人可以帮忙告诉我我要去哪里了吗?

Answer by Shim-Sao in his comments ... modifying the .htaccess file didn't work, so I edited the wp-config.php and added the following lines which worked: 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. */

Try replacing: 尝试更换:

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

with: 有:

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

You also mentioned previous attempts "keep failing". 您还提到了先前的尝试“保持失败”。 Could you tell us what is going wrong, since it may be something that will also cause the approach above to fail. 您能否告诉我们出了什么问题,因为这可能也会导致上述方法失败。

You can try : 你可以试试 :

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

or to force https : 或强制https:

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

Replace example.com by the host you want. 用所需的主机替换example.com

EDIT IMPORTANT : 编辑重要事项:

In WordPress, don't use .htaccess, use wp-config.php and adapt the 2 lines with the right host name : 在WordPress中,请勿使用.htaccess,请使用wp-config.php并使用正确的主机名修改两行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM