2

I have a WordPress website on a Bluehost shared hosting server and since I have multiple websites in my space, I have this particular folder in its own folder under public_html. This means that I have a .htaccess file in public_html, which redirects my domain to the specific website folder. I also have another .htaccess file in the website folder, which has some WordPress settings and a redirect for non-www URLs. I've tried adding this http -> https rule in both .htaccess files, but it has no effect:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>

I'm also having related issues with menu links in WordPress. Even though my WP and site addresses contain https, my menu links are rendering with http. This happens if I use a custom link with just the anchor ID or full URL. Could anything in the .htaccess file be interfering? Why would my http -> https rule be ignored?

1 Answer 1

1
RewriteCond %{HTTP_HOST} ^example\.me [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.me/$1 [R,L]

You've not stated where exactly you are adding these rules in your .htaccess file(s). The order is important. These directives need to go at the top of the .htaccess file, before the WordPress front-controller, otherwise they won't redirect anything other than static resources.

This rule will also only redirect example.com, it won't canonicalise a request for http://www.example.com/ (ie. HTTP and www) - which is presumably the intention.

To redirect non-www to www and HTTP to HTTPS you would need to include the OR flag in the first condition it's either example.com or HTTP - the default is otherwise an implicit AND. For example:

RewriteCond %{HTTP_HOST} ^example\.me [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.example.me/$1 [R,L]

You also don't need the <IfModule> wrapper or to repeat the RewriteEngine On directive if it is already present later in the file.

Ultimately this should be a 301 (permanent) redirect, so change R to R=301 once you have confirmed it works as intended.

I have an .htaccess file in public_html, which redirects my domain to the specific website folder

I assume you do literally mean a "redirect" and not an internal rewrite? In which case this is OK. However, if you are internally rewriting the request to the specific subdirectory then the above is incorrect as it will expose the subdirectory in the URL. In this case you should remove the rule from the subdirectory .htaccess file.

2
  • 1
    This rule worked. Thank you so much! I placed it at the top of my website folder's .htaccess file. It now redirects non-www and http links correctly. The only remaining issue is that my WP menu is generating links with http. Commented Aug 12, 2021 at 17:56
  • Never mind, I was looking at the wrong menu. All good now Commented Aug 12, 2021 at 18:21

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.