简体   繁体   中英

Wordpress .htaccess www redirect fail

I've just been asked to help administer a charities WordPress site. It's using http and I'd like to modify the .htaccess redirect to www. The .htaccess file looks bog standard, with the addition of a 404 at the start:

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:

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.

I'm a complete newbie to WordPress so can anyone help and tell me where I'm going wrong please?

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:

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 :

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.

EDIT IMPORTANT :

In WordPress, don't use .htaccess, use wp-config.php and adapt the 2 lines with the right host name :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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