Support » Fixing WordPress » How to correctly refer visitors to BOTH www and HTTPS using the htaccess-file

  • I think I have the correct solution, and everything seems to be working, however there seems to be half a million different ways of doing this. My site uses www before the site name. I have previously used this code (in htaccess) to direct all visitors to the www-version of the site:

    # Redirect non-www to www-version of page
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

    This has been working fine. Yesterday i did the move to HTTPS and therefore also needed rules for that in my htaccess file. Right now it looks like the code below. This seems to be working completely fine. However I am not sure that it is actually correct. I removed the old non-www to www-code and replaced it with the code below.

    Is there a better way or is this perfectly fine?

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    # Redirect all to HTTPS
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    # Redirect done
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @mrorsh,

    It’s not wise to edit the original WordPress block under .htaccess. You can code it something like below:

    # 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 Redirect http to https://www
    <IfModule mod_rewrite.c>
      ## redirect http to https
      RewriteCond %{HTTPS} off
      RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      ## redirect https to https://www
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    # END Redirect http to https://www

    Generally, you don’t need to put such rules under .htaccess to redirect http to https://www. The best method to do it by updating the site address (url) under WordPress Admin (Settings -> General) and WordPress will take care of it.

    Reference screenshot:

    I hope this helps.

    Thank you! Realised WordPress automatically overwrites any changes made inbetween the # WORDPRESS.

    I found a secound solutions that seems to be working just fine, generating 301 redirects with only one redirect with every attempt to reach the page (www, non-www, http, https). All gets forwarded to https://www. Is this also an OK way of doing the redirect through the htaccess-file. Or should I stick with your example?

    # All to HTTPS
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://www.enkelteknik.se/$1 [L,R=301]
    </IfModule>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to correctly refer visitors to BOTH www and HTTPS using the htaccess-file’ is closed to new replies.