Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want all requests on a domain to be redirected to https, with the exception of just one particular file. I'm not sure how to accomplish this with .htaccess

-bash-3.2# cat .htaccess
ErrorDocument 404 http://www.example.com
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
-bash-3.2#

The above code redirects everything perfectly, however, I need the robots.txt file to be accessible via http:// vs the https:// only.

share|improve this question
up vote 1 down vote accepted
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !robots\.txt
RewriteRule .* https://www.example.com/$0 [R,L]

Note that HTTPS is a more reliable indicator of whether or not SSL is enabled.

share|improve this answer

This would fit better in serverfault but...

You can work with a pre-condition first. Something like:

ErrorDocument 404 http:// www.domain.com

RewriteEngine On

RewriteCond %{REQUEST_URI} http://(www)?\.domain\.tld\/robots\.txt
RewriteRule http://domain\.tld\/robots\.txt [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https:// www.domain.com/$1 [R,L]
share|improve this answer
1  
-1 ... have never seen the protocol and domain name included in the %{REQUEST_URI} environment variable... – danlefree Dec 18 '10 at 10:32
    
This was just a detail. The focus should be the idea of pre-condition. Nice reply yours by the way, much more elegant. – Dave Dec 18 '10 at 15:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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