When I set my WordPress site’s address (URL) setting to = http://www.example.com, then the site will redirect all users to access the site WITH the ‘www.’ prefix:
(e.g. http://example.com/… will be redirected to http://www.example.com/…)
When I check the .htaccess file, there’s no rule for handling a missing ‘www’. How is WordPress doing this?
WordPress .htacess file:
# 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
The canonical.php file in the wp-includes folder of your WordPress install is where this particular magic happens – there’s a call to wp_redirect that sends a 301:Moved Permanently header with the canonical url.
FWIW, this is largely unrelated to the rewriting/redirection mechanism in wp-includes/rewrite.php, which handles generating the .htaccess file and saving the permalink structure rewriting rules into a WordPress option (option name ‘redirect_rules’ in wp_options).