Adding force https rule to other rewrite rules in htaccess

I understand that to force http to https I need to add the following two lines directly after RewriteEngine On line:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

However, I already have some rewrite rules. Do I just add them as suggested above or do I have to combine them with existing lines in the below:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Thank you

  • Sajid

    Hello PCI Team,
    Hope you are doing good today :slight_smile:

    This is a stand alone rule and would work fine as it is. Just add it under RewriteEngine On line without adjusting it with any other existing rules.

    If above don't work then try this one:
    https://www.thepolyglotdeveloper.com/2014/12/force-https-pages-wordpress-site/

    Hope that helps! Feel free to post a reply if you need further assistance :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • PCI Team

    Hello Sajid

    Many thanks for your response and for confirming I could add the lines without modifying anything else. It worked perfectly and now http://performanceconsultants.com redirects to https.

    I tried adding the same two lines to the htaccess file in another addon domain hosted in the same public_html folder. But it did nothing. Now I see that there are two <IfModule mod_rewrite.c> in the htaccess file (copied below) which seems odd. Should I delete one and, if so, which one?

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/wp-content/endurance-page-cache/ - [L]
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP_COOKIE} !(wordpress_test_cookie|comment_author|wp\-postpass|wordpress_logged_in|wptouch_switch_toggle|wp_woocommerce_session_) [NC]
    RewriteCond %{DOCUMENT_ROOT}/wp-content/endurance-page-cache/$1/_index.html -f
    RewriteRule ^(.*)$ /wp-content/endurance-page-cache/$1/_index.html [L]
    </IfModule>
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

  • VC Nickels

    PCI Team

    Both of those are likely as they should be...

    This part

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/wp-content/endurance-page-cache/ - [L]
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP_COOKIE} !(wordpress_test_cookie|comment_author|wp\-postpass|wordpress_logged_in|wptouch_switch_toggle|wp_woocommerce_session_) [NC]
    RewriteCond %{DOCUMENT_ROOT}/wp-content/endurance-page-cache/$1/_index.html -f
    RewriteRule ^(.*)$ /wp-content/endurance-page-cache/$1/_index.html [L]
    </IfModule>

    Is, I believe, part of BlueHosts cache system

    This part

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    Is part of WordPress and is (or should be) wrapped in # BEGIN WordPress and # END WordPress. Don't mes with anything that is in the # BEGIN WordPress section.

    Your additions should go at the top (above) and as a matter of convention are wrapped in #'s so your htaccess files should be something like

    # BEGIN PCI Additions
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # END PCI Additions

    and then what ever else is added by your host, WordPress and any plugins you have.

    NOTE: .htaccess rules are inherited so any rules in the .htaccess file on the /public_html root are followed buy all subfolders. This can have unwanted effects. It's best to keep all domains in a separate folder structure, if possible.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.