# Shared ssl cert and url rewritting

**URL:** <https://www.ruby-forum.com/t/shared-ssl-cert-and-url-rewritting/109406>\
**Category:** Rails\
**Created:** [July 14, 2007, 9:30am UTC](https://www.ruby-forum.com/t/shared-ssl-cert-and-url-rewritting/109406 "2007-07-14T09:30:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kyle](https://www.ruby-forum.com/u/kyle)\
**Post date:** [July 14, 2007, 9:30am UTC](https://www.ruby-forum.com/t/shared-ssl-cert-and-url-rewritting/109406/1 "2007-07-14T09:30:38Z")

</div>

Hello,

My client does not want to purchase a ssl cert, rather they want to  
use their host’s ([westhost.com](http://westhost.com)) shared ssl cert. To access the site  
through shared ssl under their setup you have to use the url:

[https://ssl4.westhost.com/yourdomain.com/path/to/controller](https://ssl4.westhost.com/yourdomain.com/path/to/controller)

As you can see, these makes havoc rain down on the url.

But, I googled around and came up with a good solution I thought, that  
is something similar to a subdomain redirect:

[http://www.railsonwave.com/railsonwave/2007/7/10/howto-put-a-controller-under-a-ssl-subdomain](http://www.railsonwave.com/railsonwave/2007/7/10/howto-put-a-controller-under-a-ssl-subdomain)  
(my code is below)

I modified the above code to give me a nice url when I need ssl. And  
it works, except it goes into an endless redirection loop.

I think the problem is that not only is the domain changing, but also  
the base of the url which must really screw rails up.

So, is there a way to do something like this or do I just need to lay  
down the law with the client and buy an ssl cert?

Thank You,  
kyle

code from application.rb

#rewrite the url to redirect to the westhost ssl server when needed  
#see config/environment.rb to set which controllers need ssl  
def url\_for(options = {}, \*parameters\_for\_method\_reference)  
tmp\_result = super((options.is\_a?(Hash) ?  
options.merge({:only\_path =\> false}) : options),  
parameters\_for\_method\_reference)  
if not (tmp\_result =~ Regexp.new(request.host)).nil?  
url\_to\_go = dispatch\_ssl(tmp\_result)  
end

```
  super((url_to_go.nil? ? tmp_result : url_to_go),

```

parameters\_for\_method\_reference)  
end

NOTE: SECURE\_DOMAIN is set in environment.rb to be:  
[ssl4.westhost.com/mydomain.com/](http://ssl4.westhost.com/mydomain.com/)

#called by url\_for, thus makes the proper ssl domain  
def dispatch\_ssl(tmp\_result = request.request\_uri)  
domain = SECURE\_DOMAIN.blank? ? “” : SECURE\_DOMAIN + “.”  
# need secure is a function that simply check if this path need  
to be put under SSL  
if need\_secure?(tmp\_result)  
if request.protocol == ‘http://’  
url\_to\_go = “https://#{domain}” + tmp\_result  
end  
else  
if request.protocol == ‘https://’  
host = request.host.gsub(Regexp.new(subdomain),“”)  
url\_to\_go = ‘http://’ + host + tmp\_result  
end  
end  
return url\_to\_go  
end

#used by dispatch\_ssl, checks the config/environments.rb set  
SECURE\_MODE  
#to see if the uri really does need ssl or not  
def need\_secure?(uri)  
SECURE\_MODE.each{ |s|  
a = (uri =~ s)  
return true unless a.nil?  
}  
return false;  
end

#we put this magic into a before\_filter of a controller  
#which will check if they need ssl and aren’t using it  
#and then send them marily on their way to ssl bliss.  
#we hope!  
def ssl\_required  
if not (redir = dispatch\_ssl.to\_s).blank?  
redirect\_to(redir) and return false  
end  
end
