首页 > 解决方案 > 获取 url :local/ads.txt 以重定向到 public/ads.txt 文件

问题描述

我有一个ads.txt存储在public/ads.txt其中的文件,可以通过mywebsiteurl.com/ads.txt

但我也希望使用本地参数访问此文件,例如mywebsiteurl.com/en/ads.txt

路线.rb

Rails.application.routes.draw do

    get 'ads.txt', to: redirect("/ads.txt")

    scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
       resources :offers
       ...
    end

end

当本地参数存在时,我该如何访问 ads.txt?(我试图将 get 'ads.txt' 路由放在我的 :locale 范围内,但它不起作用。有什么想法吗?

标签: ruby-on-railsruby

解决方案


您实际上并不需要get 'asd.txt'路由重定向,网络服务器(nginx、apache 等)应该从 /public 提供文件,而无需接触您的 rails 应用程序。

对于您的实际问题,您应该能够做到这一点:

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
   get 'asd.txt', redirect_to('/asd.txt') # this should match ":local/asd.txt" request

   resources :offers
   ...
end

推荐阅读