Redirect http to https and non www to www on httpd

Installing, Configuring, Troubleshooting server daemons such as Web and Mail
Post Reply
nazos
Posts: 3
Joined: 2014/12/13 17:36:38

Redirect http to https and non www to www on httpd

Post by nazos » 2014/12/13 17:40:37

As the title stays, I would like to redirect the http requests to https and non www to www using httpd.

My httpd conf file:

Code: Select all

<VirtualHost *:80>
    ServerAdmin email.domain.com
    DocumentRoot /var/www/dir/public_html
    ServerName sub.domain.com
    ServerAlias www.sub.domain.com
    #Redirect permanent / http://www.sub.domain.com
    ErrorLog /var/www/dir/error.log
</VirtualHost>

<VirtualHost *:443>
        SSLEngine on
        SSLCertificateFile /etc/ssl/sub.domain.com.crt
        SSLCertificateKeyFile /etc/ssl/sub.domain.com.key
        SSLCertificateChainFile /etc/ssl/sub_domain.ca-bundle
        <Directory /var/www/dir/public_html>
                AllowOverride All
        </Directory>
        ServerAdmin email.domain.com
        DocumentRoot /var/dir/dir/public_html
        ServerName sub.domain.com
        ServerAlias www.sub.domain.com
        #Redirect permanent / https://www.sub.domain.com/
        ErrorLog /var/www/dir/error.log
</VirtualHost>
The redirect lines are commented cause they causing problems.
I dont know what am I doing wrong. I would really appreciate your help

sam.vid
Posts: 1
Joined: 2014/12/18 13:41:42
Contact:

Re: Redirect http to https and non www to www on httpd

Post by sam.vid » 2014/12/18 14:08:36

Can you try the following:

Code: Select all

<VirtualHost *:80>
	ServerAdmin email.domain.com
	DocumentRoot /var/www/dir/public_html
	ServerName sub.domain.com
	ServerAlias www.sub.domain.com    
	ErrorLog /var/www/dir/error.log
	RewriteEngine On
	RewriteCond %{HTTPS} !on
	RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
        SSLEngine on
        SSLCertificateFile /etc/ssl/sub.domain.com.crt
        SSLCertificateKeyFile /etc/ssl/sub.domain.com.key
        SSLCertificateChainFile /etc/ssl/sub_domain.ca-bundle
        <Directory /var/www/dir/public_html>
                AllowOverride All
        </Directory>
        ServerAdmin email.domain.com
        DocumentRoot /var/dir/dir/public_html
        ServerName sub.domain.com
        ServerAlias www.sub.domain.com
        ErrorLog /var/www/dir/error.log
	RewriteEngine On
	RewriteCond %{HTTP_HOST} ^sub.domain.com$
	RewriteRule (.*) http://www.sub.domain.com/$1 [R=301,L] 
</VirtualHost>

Post Reply

Return to “CentOS 5 - Server Support”