tags:

views:

45

answers:

2

So this is what I currently have, I'm trying to redirect http://something.com to http://www.something.com but if its a subdomain do no such things, so http://other.somethings.com will stay the same AND conditional based on https/http

Currently have..

RewriteCond %{HTTP_HOST} !^www\.

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
A: 

It's not that difficult, although it is a bit tedious. There's another rewrite conditional variable called HTTPS which is safe to use even when you don't have mod_ssl running. You'd then chain two RewriteCond checks, and get something like this:

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^something\.com$
RewriteRule ^(.*)$ https://www.something.com/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^something\.com$
RewriteRule ^(.*)$ http://www.something.com/$1 [R=301,L]

The downside is that you are now hardcoding the domain in question into the .htaccess. I'm not aware of a way to make it work without hardcoding the domain.

ivans
what if its http://something.domain.com would need to stay that way
wes
Ah, my mistake... I'll edit the answer!
ivans
+1  A: 

Try this rule:

RewriteCond %{HTTP_HOST} =example.com
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This will only redirect requests to the host example.com and keep HTTPS if it’s used.

Gumbo
I'm curious... can you explain `^on(s)|`?
eyelidlessness
@eyelidlessness: The value of *HTTPS* is either `on` or `off`. So `%{HTTPS}s` is either `ons` or `offs`. If it’s `ons`, the pattern `^on(s)|` matches and the match of the first group is `s`. Otherwise, if it’s `offs`, the empty branch is taken and the match of the first group is empty.
Gumbo
Oh cool. I didn't notice the `s` at the end of `%{HTTPS}s`, that makes a lot of sense. Thanks!
eyelidlessness