1

I'm having a hard time figuring out how to properly configure a mod_rewrite rule for the following conditions:

  • redirect non-www to www AND http to https ONLY for the root path: / ie. example.com
  • disregard anything else such as /test.html ie. example.com/test.html

Note that I'm writing the rules in Apache include files within directory rules.

Here's what I'm currently using:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This correctly redirects all non-www requests to www, but it also allows for HTTP requests, which I'd like to prevent but only for / ie. example.com

Then I tried the following:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^$ https://www.example.com [L,R=301]

This (apparently) worked, but it turned out it blocked all 3rd party uptime-monitoring sites from issuing HTTPS get requests, even though it was working fine in a regular browser. So I'm out of ideas.

  • what many people forget is that www is a hostname/ServerName – Sum1sAdmin Aug 24 '18 at 11:09
  • 1
    "it blocked all 3rd party uptime-monitoring sites from issuing HTTPS get requests" - You need to know why it blocked these 3rd party monitoring sites (there's no good reason why it should). However, if the monitoring site is checking example.com (no www) then yes, the "redirect" might effectively "block" the monitoring site. Or, you could implement an exception, although that should be unnecessary I would have thought. Your second rule block should be OK. (Your first rule block specifically maintains the protocol on the original request - which is not what you require.) – MrWhite Aug 24 '18 at 11:59
  • 1
    Just to clarify...If the monitoring site is being "blocked" by the www "redirect", because the incorrect hostname is being monitored, then you should modify the URL being monitored, not include an exception in your current rule block. As Sum1sAdmin hinted at, if these directives are in your server-config then they can probably be simplified (depending on how you have configured your VirtualHosts, and exactly where these directives are being included). For example, a single RedirectMatch directive might be all that's required. – MrWhite Aug 24 '18 at 12:15
  • 1
    Thanks for your input, much appreciated. I'm also suspecting there's something wrong with the monitoring site so for now I've contacted them regarding this issue and waiting for their reply. Will update this post with the progress. – Ivan Aug 24 '18 at 15:36
1

As it turned out, it was indeed a case of the 3rd party monitoring sites not following redirects properly, as suggested by @MrWhite. I solved it by editing the monitoring path to check via HTTPS protocol directly, thus avoiding redirects.

The rewrite rule was correct after all, which I ended up using only for the root path being included in Apache per directory server directives:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^$ https://www.example.com [L,R=301]

By the way, for those struggling with htaccess/mod_rewrite rules, here's an awesome online tool that's helped me with testing a lot: https://htaccess.madewithlove.be/

Thank you everyone for your inputs. Much appreciated.

  • "monitoring sites not following redirects properly" - although you wouldn't expect a monitoring site to follow redirects. "here's an awesome online tool" - Whilst that tool is undoubtedly convenient, I would exercise caution with the results. Many questions have cited that tool and the results they are getting are clearly different to what the tool is reporting. – MrWhite Aug 26 '18 at 11:47
  • The monitoring site which I was using had the option Enabled to "follow redirects". Obviously that wasn't working properly for some reason. I shared the tool because, at least in my case, it worked. I have found it much more convenient and especially faster than restarting the webserver after each modification then clearing the browser cache just to check if a rule modification works the way it should. – Ivan Aug 27 '18 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.