.htaccess Canonical Wordpress

SeoReborn

New member
Mar 5, 2008
2,216
28
0
London UK
I'm trying to get a wordpress site to redirect to the www version using the .htaccess. The following code gives me 404 error

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

It works in all of my non wordpress sites but can't get it to work for wordpress.

Help please?
 


Try this:

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


 
the pages says

Not Found

The requested URL /403.shtml/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
 
It sounds like you have a different configuration issue somewhere, and you can confirm by manually accessing the "www" version of the URL... does that also give you the 403 error?

403 is a "Forbidden" error, so you could be having issues with permissions on the target file, apache config that denies access, or incorrect selinux context (should be httpd_sys_content_t)
 
I'm pretty sure you need to change the domain in the database for your WordPress installation. Try changing the domain in the wp_options table. I think siteurl is the field you need to edit. There may be one or two other fields that need to be updated, but you would have to look through all the fields in the wp_options table to check.

You might also be able to do this from your WordPress control panel, but I'm not sure if it updates the domain everywhere that needs to be updated. Try changing the domain by going to General (under settings) and entering the correct domain in the field labeled "WordPress address (URL)".
 
  • Like
Reactions: SeoReborn
Finally! I did what you said and it worked! So its all to do with the settings rather than messing with the .htaccess. Thanks cziffa. +rep!
 
had the same trouble today while setting up the new WP site. Currently managed to get it working but in my case, i set it to redirect www to non-www.

whats a good setup? redirecting www to non-www or vice versa?