Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want whenever we access site-domain.com it should be redirected to https://www.site-domain.com/users/sign_in.

Here is my config

<VirtualHost *:443>
ServerName www.site-domain.com
DocumentRoot /var/www/current/public

RailsEnv copsvpc

SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /etc/apache2/ssl/certs/cert.crt
SSLCertificateKeyFile /etc/apache2/ssl/private/site-domainV2.key
SSLCertificateChainFile /etc/apache2/ssl/certs/inertermediateCA.crt

<Directory /var/www/current/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

<Directory /var/www/current/public/Help>
RewriteEngine on
RewriteCond %{HTTP:Cookie} !help=([a-zA-Z0-9]+)
RewriteRule ^(.*)$ /users/sign_in [L,R]
</Directory>

</VirtualHost>

<VirtualHost *:80>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>   

Should I simply add the following

ServerAlias site-domain.com
RewriteEngine On
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

I can not test this as site can not go down.

So it would be like

<VirtualHost *:443>
ServerName www.site-domain.com
DocumentRoot /var/www/current/public
ServerAlias site-domain.com

RailsEnv copsvpc

SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /etc/apache2/ssl/certs/cert.crt
SSLCertificateKeyFile /etc/apache2/ssl/private/site-domainV2.key
SSLCertificateChainFile /etc/apache2/ssl/certs/inertermediateCA.crt

RewriteEngine On
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]


<Directory /var/www/current/public>
 Options Indexes FollowSymLinks MultiViews
 AllowOverride None
 Order allow,deny
 Allow from all
</Directory>

<Directory /var/www/current/public/Help>
 RewriteEngine on
 RewriteCond %{HTTP:Cookie} !help=([a-zA-Z0-9]+)
 RewriteRule ^(.*)$ /users/sign_in [L,R]
</Directory>

</VirtualHost>

I can ping www.site-domain.com but not site-domain.com. Do we need an entry for site-domain.com in DNS as well? I would really appreciate any help.

Thanks in advance

share|improve this question
    
I have a site working with site-domain.com. Whenever we access www.site-domain.com it redirects to site-domain.com/users/sign_in as per setting –  smk May 23 '13 at 11:50
1  
Yes you will need DNS for both domains. Consider using a CNAME record for the WWW version so you only have 1 A record to manage. Also, when asking questions, try to state the current problem and the desired outcome near the top. In this case, since you could not even ping the domain all of your apache configuration information was somewhat irrelevant. –  jeffatrackaid May 23 '13 at 13:34
    
I was thinking this is what something handled by apache itself when we declare serverAlias. –  smk May 23 '13 at 13:43

3 Answers 3

Yes, you need the DNS entry both www. and domain.com should point at your website IP.

If it does not resolve it's not going to work.

share|improve this answer
 $ vi .htaccess

Append following config code:

  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^domain\.com
  RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L] 

Save and close the file. Above code will redirect users to www.domain.com url.

share|improve this answer

There are multiple steps here to get the desired outcome.

  • You must have DNS for both the www and non-www versions of the domain.

  • You will need to configure Apache to resolve both domains. By using Apache's DomainName and ServerAlias options.

    ServerName domain.comServerAlias www.domain.com

  • Then you will need to use Abishek's rules to force the redirect.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain\.com
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
  • If you are using SSL, then you will need a SSL cert that provides coverage for both www and the non-www versions of the domain. Most vendors will include domain.com if you order www.domain.com.

share|improve this answer

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.