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 need to get mod_rewrite to do the following

http://abc.com -> https://www.abc.com
https://abc.com/login -> https://www.abc.com/login
http://www.abc.com?x=3 -> https://www.abc.com?x=3
https://abc.com/login?x=1 -> https://www.abc.com/login?x=1
http://www.abc.com/login?x=3 -> https://www.abc.com/login 

I have the following, in my abc.com.conf vhost file but its not work correctly

<VirtualHost  *:80>
ServerName abc.com
ServerAlias www.abc.com        

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

RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.abc.com/%{REQUEST_URI}

</VirtualHost>

<VirtualHost  *:443>
ServerName abc.com
ServerAlias www.abc.com        

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


 </VirtualHost>

Can anyone help me out on the rest?

[edited to clarify]

share|improve this question
    
Can you clarify how it's not working? And where are these rules defined? –  Shane Madden Sep 11 '12 at 4:42
    
Edited to clarify –  Jo Erlang Sep 11 '12 at 5:22
add comment

1 Answer

First of all it looks like you want to force ssl for all.. I would take care of that first in your *:80 block:

<VirtualHost *:80>

  ServerName abc.com
  ServerAlias www.abc.com        

  Redirect permanent / https://www.abc.com/

</VirtualHost>

Then do your *:443 .. However you can make it real easy and just pull out abc.com into a separate vhost:

NameVirtualHost *:80
NameVirtualHost *:443

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

<VirtualHost *:443>
  ServerName abc.com
  Redirect permanent / https://www.abc.com/
   ....
    ...
   # Note you need a wildcard cert.. I assue you have one already or are using self signed. 
</VirtualHost>

Ok now you redirected all your abc.com to www.abc.com and you can set up this vhost as usual without all the rewrites.

<VirtualHost *:80>

  ServerName www.abc.com        
  Redirect permanent / https://www.abc.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName www.abc.com
  ...
   ....
</VirtualHost>
share|improve this answer
add comment

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.