I got a small site which served by Apache (I can't put Nginx in front nor change Apache to anything), and it is set up to serve the same site both over http and https (no redirects http->https is there so far, so both http and https versions are served in parallel).

What I need is to set up .htaccess so the same URI via http and via https to serve different text file?

Like http://example.com/proto.txt says "The site is over http" while https://example.com/proto.txt would say "The site served over https".

  • Don't quite have the time to check the exact way to write this (and so just a comment, not an answer), but another option if you don't want to or can't change the main Apache config, is to use a RewriteRule in your .htaccess with a condition on it being served over https. – jcaron Nov 26 '18 at 12:45
  • @dcaron, this is exactly I need, I just can't figure out how to do that exactly, and need an advice or (better) code snippet to do that :) – Kevin M Nov 27 '18 at 10:24
  • Your http and https sites are in different <VirtualHost>s. So you simply need to configure a different robots.txt in one of them. Around so: RewriteRule ^/robots.txt$ /path/to/alternative/robots.txt [L] – peterh - Reinstate Monica Dec 12 '18 at 18:08

Use an Alias

Create two files, robots.txt and robots_http.txt and add this to your http VirtualHost:

Alias "/robots.txt" "/path/to/documentroot/robots_http.txt"
  • Can not modify vhost settings, can only edit .htaccess, this is the trick. – Kevin M Nov 27 '18 at 10:11

If you can't or won't change the "main" Apache config but need to do it in a .htaccess file, you can use a RewriteRule with a RewriteCond that checks for HTTPS.

Something along the lines of:

RewriteEngine On

RewriteCond %{HTTPS} "on"
RewriteRule robots.txt robots_https.txt [L]

should probably work (I didn't test it).

Note that this is based on Apache doing HTTPS termination itself. If HTTPS termination is done on a reverse proxy before it, then the condition will likely be different (and will depend on the configuration of the reverse proxy and Apache).

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.