允许访问子域上的一个页面,如果尝试访问其他页面则重定向到主域

allow access to one page on sub-domain, and if tried accessing other pages redirect to main domain

允许访问子域(aap.example.com)上的一个页面(campaigns/test),如果尝试访问其他页面则重定向到主域(example.com

app.example.com 是我的主域 example.com 的子域,所以同一个 rails 应用在域和子域上是 运行。

我也尝试了一些解决方案,比如在路由文件和 nginx 文件中添加约束,但没有得到我需要的。

routes.rb

constraints(Subdomain) do match '/campaigns/:slug' => 'campaigns#show', via: [:get] match "/cam_enquiry" => "campaigns#cam_enquiry", via: [:post] end

subdomain.rb模块

class Subdomain def self.matches?(request) case request.subdomain when 'app', '', nil false else true end end end

如果您需要更多详细信息,请告诉我。

定义所有路由,然后定义 app 子域的约束。您需要匹配 test 路由并为所有其他子域路由使用重定向。在重定向参数中仅指定空白子域,在这种情况下它将从请求中获取所有其他信息并且仅将子域更改为主域

get 'campaigns/test', to: redirect(subdomain: 'app'), constraints: { subdomain: '' }
get '/campaigns/:slug', to: 'campaigns#show'
post '/cam_enquiry', to: 'campaigns#cam_enquiry'

constraints subdomain: 'app' do
  get 'campaigns/test', to: 'contoroller#action'
  get '/*any', to: redirect(subdomain: '')
end