How redirect all pages to ‘https’ using htaccess and WordPress with a subdomain?

I need to redirect all “http” requests to “https”. I did something like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://example.com.br/site/$1 [R=301,L]
</IfModule>

It seems work appropriately, but this is not loading files. Files should be opened using the url:

Read More
https://example.com.br/site/wp-content/themes/briefing/css/style.css

but instead, the page try to load:

https://example.com.br/site/css/style.css

“https://” does not appear in Browser. I am using WordPress, the site is inside a “site” folder. My big problem is because I have a ajax request and had the following error:

XMLHttpRequest cannot load https://exemple.com.br/site/wp-content/themes/briefing/includes/loopHandler.php?numPosts=3&pageNumber=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com.br' is therefore not allowed access. 

My htacess file begins with:

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

What should I do?

Related posts

Leave a Reply

3 comments

  1. You need to replace your RewriteCond:

    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    

    by the following:

    RewriteCond %{HTTPS} !on    
    

    The HTTP:X-Forwarded-Proto you currently using only checks if a proxy forwarded non https traffic. This is not really related to your problem. You need a general check if incoming requests are not https requests: %{HTTPS} !on.

  2. You may can try that:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        RewriteEngine On
        RewriteCond %{SERVER_PORT} 80
        RewriteRule ^(.*)$ https://www.mysite.com/my-sub-domain/$1 [R,L]
    </IfModule>
    # END WordPress
    
  3. Keep your rules like this:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /site/
    
    RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://example.com.br%{REQUEST_URI} [R=301,L,NE]
    
    RewriteRule ^index.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /site/index.php [L]
    
    </IfModule>
    
    • Then do make sure your home and site URLs in WP’s permalink settings also has https:// URLs.