How to redirect to www? #1214
Replies: 9 comments 5 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:
|
Beta Was this translation helpful? Give feedback.
-
Okay so then my app responds on both hosts and I guess I just redirect in my application controller? |
Beta Was this translation helpful? Give feedback.
-
Okay thanks for your help. That's too bad that Kamal Proxy doesn't handle something as basic as a redirect to www. |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
The only way to use |
Beta Was this translation helpful? Give feedback.
-
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: |
Beta Was this translation helpful? Give feedback.
-
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
end This 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.de This way, Kamal will bind to both domains and accept traffic from both domain.de and www.domain.de. Note
|
Beta Was this translation helpful? Give feedback.
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?
Beta Was this translation helpful? Give feedback.
All reactions