2

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>

1 Answer 1

1

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]
7
  • Thanks for the reply. im not sure dont think that works.
    – lee
    Aug 11, 2015 at 15:39
  • Maybe just a typo, but your question states example.com, but your code states example.co.uk? Also, clear your browser cache etc. if you have previously been testing with 301 redirects.
    – MrWhite
    Aug 11, 2015 at 16:24
  • Also, it's possible that SERVER_PORT is not set correctly, so HTTPS could be used instead. I've updated my answer. (However, this probably won't affect your stated example, https://example.com to https://www.example.com since that should be triggered by the very first condition.)
    – MrWhite
    Aug 11, 2015 at 16:45
  • hi sorry w3d yes you are correct, my website is .co.uk and not .com i did not think it would make a difference, also when i did the first suggestion all my pages redirected to my homepage
    – lee
    Aug 12, 2015 at 10:44
  • Well, if the domain is supposed to be one thing but you code for another then that is going to make a difference! The "first suggestion" cannot redirect all pages to your homepage - in fact, it is no different to your original code in this respect. If that is happening then there is something else going on. Try clearing browser cache etc.
    – MrWhite
    Aug 12, 2015 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.