0

I am trying to redirect all the requests to https www version of the website. Homepage redirects perfectly fine but any other page of the website doesn't. I checked the redirect rules mentioned in the htaccess and they work perfectly fine. However, on the live website, it doesnt.

Here's the redirect rules

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]

Any reason why this is happening?

I tried working through different redirect rules to see which one redirects as per the requirement.

  • https://stackoverflow.com/a/33389670/1844933 – user1844933 Feb 14 '23 at 05:13
  • Tried using the ones mentioned. Didn't solve the issue. Can you attach the particular code snippet? – Jainam Desai Feb 14 '23 at 06:26
  • No obvious reason why this shouldn't work. Is that all you have in terms of rewrite configuration, or is there more? – CBroe Feb 14 '23 at 06:47
  • Could it be that you are simply looking at prior results cached on the client side? Always test using a fresh anonymous browser window ... – arkascha Feb 14 '23 at 06:55
  • Checked in incog/guest window. And there's nothing else other than this in htaccess file. – Jainam Desai Feb 14 '23 at 09:54
  • "in htaccess file" - To clarify, the file is named `.htaccess` (with a dot prefix)? If you remove this rule (and clear your cache) does the homepage redirect stop? We'll need to know more about your setup... is this an entirely static site, serving static files? Is your site behind a proxy that serves static files? What version of Apache are you on? – MrWhite Feb 14 '23 at 10:57

1 Answers1

0

try this

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.yourSiteName.com/$1 [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.yourSiteName.com/$1 [L,R=301] 
user1844933
  • 3,296
  • 2
  • 25
  • 42
  • first non www to www and to https – user1844933 Feb 14 '23 at 08:25
  • It goes in a never-ending loop. I checked it on https://wheregoes.com/ – Jainam Desai Feb 14 '23 at 09:52
  • The first rule is basically the same as the rule in the question (if the rule in the question isn't working as intended then this is not going to make any difference). The "never-ending loop" is probably caused by the second rule and the check against the `HTTPS` server var. But that could be a clue as to why the original rule is not work for subpages... if you are behind a proxy server of some kind? @JainamDesai – MrWhite Feb 15 '23 at 00:19