Mobile app version of vmapp.org
Login or Join
Martha676

: Wordpress https://example.com not redirecting to https://www.example.com htaccess I'm using WordPress and moved to https recently. I've had some issues since with the domain not redirecting properly.

@Martha676

Posted in: #Htaccess #NoWww #Wordpress

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 example.com is not redirecting to 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 ^(.*)$ www.example.co.uk/ [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>

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Martha676

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

It looks like you are just missing the OR flag:

RewriteCond %{HTTP_HOST} ^example.co.uk [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.example.co.uk/ [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 ^(.*)$ www.example.com/ [R,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme