11

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".

Daniele Santi
  • 2,479
  • 1
  • 25
  • 22
Kevin M
  • 299
  • 3
  • 10
  • 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 ``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 Dec 12 '18 at 18:08

2 Answers2

19

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"
Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
2

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).

jcaron
  • 985
  • 6
  • 9