3

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.

MrWhite
  • 42,784
  • 4
  • 49
  • 90
bdwarr6
  • 33
  • 2

2 Answers2

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

MrWhite
  • 42,784
  • 4
  • 49
  • 90
danlefree
  • 12,838
  • 4
  • 42
  • 59
-1

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]
Davis Peixoto
  • 2,766
  • 14
  • 14
  • 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. – Davis Peixoto Dec 18 '10 at 15:29