I need to redirect help.mydomain.com/a-page to /pages/a-page without using Nginx or Apache redirects and only through Rails 3 routes.
The key here is the help subdomain determining that /pages/ should be dropped from the route although the view is under the /views/pages/
Any help is appreciated.
Try this:
match "/:page_name" => redirect("/pages/%{page_name}"), :constraints => { :subdomain => /help/ }
Related
I have several static erb pages being served up in a ruby rails 4 site via the high voltage gem:
get '/about' => 'high_voltage/pages#show', id: 'about'
get '/contact' => 'high_voltage/pages#show', id: 'contact', :protocol => "https"
get '/privacy' => 'high_voltage/pages#show', id: 'privacy'
This all works well and good, except that the /contact route doesn't redirect or force SSL on, it is happy with whatever protocol is used.
I host the site on engine yard, attempting to put :force_ssl only or variants in the route line resulted in failed deployments - high voltage uses a slightly different set of arguments than normal routes so I suspect there is a conflict somewhere.
Anyone use highvoltage and SSL with rails 4 for select static pages (not the whole site)? Example routes line please.
You can achieve this by overriding the HighVoltage#PagesController see the override section of the documentation.
It might look something like this:
class PagesController < ApplicationController
include HighVoltage::StaticPage
before_filter :ensure_secure_page
private
def ensure_secure_page
if params[:id] == 'contact'
# check to make sure SSL is being use. Redirect to secure page if not.
end
end
end
Next disable the routes that HighVoltage provides:
# config/initializers/high_voltage.rb
HighVoltage.routes = false
Then in your application's routes file you'll need to set up a new route:
# config/routes.rb
get "/pages/*id" => 'pages#show', as: :page, format: false
I've got urlManager rules:
...
'/<slug:(nasha-glavnaya-zagolovok)>' => 'page/page/show',
'/pages/<slug>' => 'page/page/show',
...
all fine.
When I enter to address
/nasha-glavnaya-zagolovok
it opens the page i need
But if I try to open page
/pages/nasha-glavnaya-zagolovok
it also opens the page I need
But content of pages are same.
How i can redirect (with 301 header) from route to pattern if any rule in urlManager mathced?
i cannot give you the complete answer, but you can work make it work like this...
you can use a rule that sends the request to an action which job is just to redirect using CController::redirect()
replace this
'/<slug:(nasha-glavnaya-zagolovok)>' => 'page/page/show',
'/pages/<slug>' => 'page/page/show',
with this
'/<slug:(nasha-glavnaya-zagolovok)>' => 'page/page/show',
'/pages/<slug>' => 'site/redirect',
and redirect the user to /$slug in the 'site/redirect' action
update: you can make it work by writing a new url rule which extends CBaseUrlRule, not sure if you care to take the time to do that if this is the only redirect you want to do though
I recently added a subdomain constraint to my Rails routes file
constraints(:subdomain => 'new') do
devise_for :customers do
get "/customers/sign_up" => "registrations#new"
post "/customers" => "registrations#create"
put "/customers/:id" => "registrations#update"
end
match '/' => 'roxy#index'
namespace :roxy, :path => '/' do
resources :customers
resources :surveys
end
end
In order to test the subdomain routing constraint locally, I added this line to my hosts file.
127.0.0.1 new.localhost.local
Now, I test my app in my browser at the URL new.localhost.local:3000. It takes about 10 - 15 seconds to load every page, which is unreasonably slow. If I remove the subdomain constraint and just go to 127.0.0.1:3000, everything is zippy and fast again.
What am I doing wrong? I'm new to Rails, so please tell me if there is a better way to do subdomain routing in rails, or if there is a setting I need to configure.
Figured it out. It's nothing to do with Rails or subdomains or thin. Turns out, unlike other unixy-things, OS X reserves the .local TLD for mDNS functionality. For every page, the DNS resolution was timing out before loading my app. So I just changed my /etc/hosts file to
127.0.0.1 new.localhost.dev
and everything's working great now.
Read more: http://www.justincarmony.com/blog/2011/07/27/mac-os-x-lion-etc-hosts-bugs-and-dns-resolution/
Is there a way to set the default root routes in rails to be one of my controller action and showing in the url rather than the root "/" ?
Say I have a "Computer" controller with a "index" action.
When my user login to my application, I want the url to be
http://localhost:3000/computer/index rather than http://localhost:3000/
root :to => "computers#index"
does the latter one, how can I make the default root url to be something like the prior one ?
UPDATE: a better way would be
root :to => redirect(/path)
The simplest way would be to change which URL your users get re-directed to after they successfully log in.
You could also add a forced re-direct in your controller:
# routes.rb
root :to => "computers#force_redirect"
# computers_controller.rb
def force_redirect
redirect_to '/computers/index'
end
I am on abc.domainname.com
Now in any action
I want to redirect to a page domainname.com without subdomain.
How can I do this?
I tried like index_path(:subdomain => false).
If I hardcode like redirect_to("http://domainname.com"). Then it works.
I did in this way,
index_url(:subdomain => false)
And it works!