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

3
  • 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. Commented Nov 26, 2018 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 :) Commented Nov 27, 2018 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] Commented Dec 12, 2018 at 18:08

2 Answers 2

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" 
1
  • Can not modify vhost settings, can only edit .htaccess, this is the trick. Commented Nov 27, 2018 at 10:11
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).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.