2

I have two domains at the moment: example.pl and example.com.

I'm trying to make sure they all go to https://example.pl or http://example.com respectively but I have a problem (or my browser also remembers old state).

I have this code at the moment:


RewriteCond %{ENV:HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^example.pl [NC]
RewriteRule ^ https://example.pl%{REQUEST_URI} [L,NE,R=301]

RewriteCond %{ENV:HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]

But I'm not sure if it's working correctly (http://example.com redirects somehow to https://example.pl) and also whether this can be done in one block of code?

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Moseleyi
  • 2,585
  • 1
  • 24
  • 46

1 Answers1

0
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^example.com [NC]

Both these conditions cannot be successful, so the rule will ultimately fail (do nothing) if requesting the www subdomain over HTTPS.

If it's just two domains then you need to modify the 3rd condition to allow an optional www subdomain and use regex alternation to match the two domains.

For example:

RewriteCond %{ENV:HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com|example\.pl) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

The %1 backreference matches the captured subpattern in the last matched RewriteCond directive, ie. either example.com or example.pl. The important point is that the 3rd condition is always successful, since the only purpose of this 3rd condition is to capture the domain name.

You will need to clear your browser cache since any (erroneous) 301 (permanent) redirects will have been cached by the browser.

But I'm not sure if it's working correctly (http://example.com redirects somehow to https://example.pl) and also whether this can be done in one block of code?

That's not possible with the rules you've posted. It's possible you are seeing a cached redirect from an earlier (erroneous) 301 permanent redirect.

See my earlier answer for an any domain solution.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 1
    I disabled the cache in Chrome developer tools and even removed htaccess from the server and it still remembered the .com to .pl redirection. Not sure how it can remember this redirect – Moseleyi Jan 26 '23 at 13:33
  • Thanks @MrWhite, can you tell me what `NE` means? It's first time I've seen it? Is it also possible to add rules/condition like removing a trailing slash? – Moseleyi Jan 28 '23 at 16:50
  • @Moseleyi (Presumably you were able to "clear" the erroneous redirect or find out where that was coming from in the end?) The `NE` (`noescape`) flag prevents any "special characters" that are already URL encoded in the request being doubly URL encoded in the response. Whether you need this flag is dependent on the "complexity" of your URLs. "Is it also possible to add rules/condition like removing a trailing slash?" - Yes, that would be a separate rule. – MrWhite Jan 29 '23 at 00:14
  • Yes, I think now my cache was gone and the `http://example.com` redirects correctly. Can I trouble you to have a look at this issue I have which is related to htaccess rewriting as well?: https://stackoverflow.com/questions/75277485/htaccess-redirection-with-request-uri-domain-folders-added-to-url – Moseleyi Jan 29 '23 at 18:46