Repository navigation
How to redirect to www? #1214
Replies: 11 comments 8 replies
|
By default, Kamal Proxy routes requests only to matching hosts and does not currently support redirection. However, you can achieve this functionality as such: |
|
Okay so then my app responds on both hosts and I guess I just redirect in my application controller? |
|
Okay thanks for your help. That's too bad that Kamal Proxy doesn't handle something as basic as a redirect to www. |
|
I've been looking for this as well, kind of like what we would do in Apache .htaccess If I was to look at adding this in would there be appetite for this feature? |
|
Redirects shouldn’t be a concern of Kamal 2. These are better handled by DNS, a reverse proxy, or the app itself, depending on the use case. |
|
Of those 3 options, I think option 2 is best. Not all DNS providers offer redirection. Checking the host and redirecting at the app level means every request takes a small performance hit. So that leaves number two— which when using Kamal 2 is kamal-proxy. So I believe this should be the role of kamal-proxy (but it currently doesn’t offer this feature).On Nov 24, 2024, at 10:42 AM, Viktor Schmidt ***@***.***> wrote:
Redirects shouldn’t be a concern of Kamal 2. These are better handled by DNS, a reverse proxy, or the app itself, depending on the use case.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
|
The only way to use |
|
we like separating the behavior of redirecting from naked domain with the www domain so we just opted on setting up a server that's main purpose is to redirect: we have since made a project that's kamal deployable and you can deploy to your own $5 server to just redirect domains. super simple: |
|
Here is the final solution to redirect www to the root domain, or vice versa, when deploying with Kamal:
# app/controllers/application_controller.rb
before_action :redirect_host
before_action :redirect_www
private
def redirect_host
preferred_host = "domain.de"
if request.host != preferred_host
redirect_to "#{request.protocol}#{preferred_host}#{request.fullpath}", status: :moved_permanently
end
end
def redirect_www
if request.host.start_with?("www.")
redirect_to "#{request.protocol}#{request.host.sub(/^www\./, '')}#{request.fullpath}", status: :moved_permanently
end
endThis ensures that any requests coming to www.domain.de are redirected to domain.de, or vice versa, with a 301 Moved Permanently status.
# config/deploy.yml
hosts:
- domain.de
- www.domain.deThis way, Kamal will bind to both domains and accept traffic from both domain.de and www.domain.de. Note
|
|
For now, I'm just adding this at the top of my # Redirect www to naked domain
if Rails.env.production?
constraints(host: /\Awww\./) do
match "(*path)", via: :all, to: redirect { |_params, req|
"https://#{req.host.sub(/\Awww\./, '')}#{req.fullpath}"
}, status: 301
end
end |
|
hey, i just want to tell that i decided to make a ruby gem for redirecting www prefix in Rails. It also have a few improvements (like validating locales). It was originally created to keep the project simple, without nginx or caddy... I dont really like to place that logic into ApplicationController or Rails Router, so i just made middleware which is more convenient way (in my opinion). # config/application.rb
#
# by default it removes www. prefix
config.middleware.use Rack::UrlCanonicalizer
# or you can enforce www.
config.middleware.use Rack::UrlCanonicalizer, enforce_www: trueGlad to see some comments and suggestions |
Uh oh!
There was an error while loading. Please reload this page.
I've got Kamal 2 working great and deploying my app to www.example.com. But if a user directly navigates to example.com without "www", they hit an ugly "connection is not secure" error message. How can I get Kamal Proxy to do what (in my humble opinion) it should already be doing by default?
All reactions