събота, 29 септември 2012 г.

Apache: Redirect HTTP requests to HTTPS

Today I want to discuss a problem about a redirecting HTTP web page to HTTPS secure location. My scenario is a web server which by default is working only on HTTPS, to be able to redirect an HTTP request to HTTPS one. In the end, when you contact the web server either with HTTP or HTTPS protocol, you connect only through HTTPS protocol for secure communication. I use apache web server.  
There are two possible solutions - one with mod_rewrite, which according to apache wiki is not the recommended method (ApacheWiki mod_rewrite) and the other one, which I`m after, is using the "Redirect" directive (ApacheWiki RedirectSSL). I don`t use .htaccess files in my scenario, so the solution should be in tweaking the httpd.conf.
In this case, I`ll use CentOS 6 server with apache 2.2, installed from the binary package that comes with the distribution. The SSL configuration files can be found under the directory /etc/httpd/conf.d/ssl.conf, and the default configuration file httpd.conf is inside /etc/httpd/conf directory. I assume you have already created, or buy a SSL certificate and you can contact your server at address https://www.mydomain.com.
So first you should create a new VirtualHost configuration, which should listen and accept requests through HTTP, port 80. I choose to add this definition at the end of my httpd.conf, instead of creating a new file. That`s because it will be only couple of lines and it`s idea is only to redirect traffic to secured location. I used the example provided in apache wiki:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent / https://www.example.com/
</VirtualHost>

After you inserted it in the end of the httpd.conf and save it, check for errors the configuration with:

# apachectl configtest

Now you have added the VirtualHost configuration, but if you restart apache you will see that nothing happens, even I found that my server doesn`t listen on port 80:

# netstat -patune | grep "80"

So I should instruct apache to listen at port 80 with Listen directive, but what will happen to the SSL configuration, there is a "Listen 443" line inside /etc/httpd/conf.d/ssl.conf. Actually the server continues to listen to port 443, because of the default ssl VirtualHost configuration. In addition I add the SSLRequireSSL and SSLOptions directives to my website root (as suggested here) in /etc/httpd/conf/httpd.conf:

.....
Listen 80
.....
<Directory "/var/www/html/">
.....
SSLRequireSSL
SSLOptions +StrictRequire
.....
</Directory>

.....

More info about these directives can be found here.
Check again for errors after the changes and if "Syntax OK" restart the apache:

 # apachectl configtest
 # service apache restart


One more thing not to forget is to check your firewall and open port 80, in CentOS the easiest way is with the command:

 # system-config-firewall-tui

Note: In order the command to work you should install the package with the same name.

1 коментар:

  1. Bonus No Deposit Bonus: 100 Free Spins for Free + Extra Spins
    Casino Bonus No Deposit – Get 100 Free 포커 에이스 Spins 먹튀 폴리스 검증 업체 for $10 with 카드 게임 종류 the #1 커뮤니티 사이트 Casino Bonus Site in Canada. Bonus #1: 아이 벳 25 $1000 deposit match + 50 free spins on any

    ОтговорИзтриване