Wordpress https://example.com not redirecting to https://www.example.com htaccess

Wordpress https://example.com not redirecting to https://www.example.com htaccess - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, wordpress, no-www, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I'm using WordPress and moved to https recently. I've had some issues since with the domain not redirecting properly.



Currently it works OK but https://example.com is not redirecting to https://www.example.com.



I have tried a lot of ways but I have not been successful so far.



Here is my htaccess:



RewriteEngine On 
RewriteCond %HTTP_HOST ^example.co.uk [NC]
RewriteCond %SERVER_PORT 80
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R,L]
# 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
# BEGIN rlrssslReallySimpleSSL rsssl_version[2.1.12]
# END rlrssslReallySimpleSSL
<IfModule mod_expires.c>
ExpiresActive on

ExpiresByType image/
jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
</IfModule>

Solution :

It looks like you are just missing the OR flag:



RewriteCond %HTTP_HOST ^example.co.uk [NC,OR]
RewriteCond %SERVER_PORT 80
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R,L]


Either the bare domain is accessed OR you are accessing via HTTP then redirect. The default operator is to AND the conditions.



Also, escape the dots in your regex.



Change R (temporary) to R=301 (permanent) when you are sure it's working OK.



UPDATE: It's possible that SERVER_PORT might not be reporting the correct port (See UseCanonicalPhysicalPort directive). Alternatively you could use the HTTPS variable instead:



RewriteCond %HTTPS off


Another alternative is to test whether the host does not start www, rather than testing for equality. So, in summary:



RewriteCond %HTTP_HOST !^www. [NC,OR]
RewriteCond %HTTPS off
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect