How to redirect just the sub dir and not the content? - http-redirect

I have the following setup:
http://example.com/foo/cake1.php
http://example.com/foo/cake2.php
http://example.com/foo/cake3.php
And when I user visits:
http://example.com/foo
I want to send them to:
http://example.com
And preserve
http://example.com/foo/cake[1-3].php
etc.
I've tried:
Redirect /foo /
But that redirects everything under foo. I only want to redirect foo to the root. How can I do this?

Finally figured it out with:
RedirectMatch 301 ^/foo/$ /

Related

How to get the hostname or entire url of the visitor website in Ruby on Rails

If the user clicks a link on http://facebook.com/my_path/ that points to http://example.com/ the value of request.domain will be example.com
But in my case, I would like to get the visitor's host name facebook.com and entire url http://facebook.com/my_path/
How to get that? Any help would be appreciated. Thanks!
Use request.referer, it is from a library called Rack, which Rails itself uses, more info here https://apidock.com/rails/Rack/Request/referer
Can you try putting debugger in the controller and access that controller in your browser. When you reach the byebug shell, try to check the output of the request variable like below
(byebug) request
#<ActionDispatch::Request:0x007fe49c151840 #env={"rack.version"=>[1, 3], "rack.errors"=>#<
the output should have your referrer path if it exists

Rails configure application to have url prefix

I feel like this should not be all that difficult, but I cannot find any solutions that seem to work with Rails4
My setup
I have a proxy server (Kong) that directs to various services behind it based on path.
myapp.com/app1/ is redirected to http://app1_address:PORT/ (notice /app1 is stripped)
same for myapp.com/app2
app2 is a Rails 4 application and it works just fine when browsing to specific, but its relative routing is completely off. For example, link_to or url_for links to controllers or actions all redirect to the wrong links.
For example, I have a logout link that has a path of /logout on app2, but redirecting the user to /logout is incorrect. They need to be routed to /app2/logout. How can I configure the Rails app to add a prefix to all routes?
I have tried:
config.action_controller.relative_url_root = '/app2'
As well as this:
config.relative_url_root = '/app2'
And this in config.ru
map <MyAppName>::Application.config.relative_url_root || "/" do
run Rails.application
end
Any ideas for how to make this work?
You should use a scope in your routes:
Rails.application.routes.draw do
scope ENV["PROXY_PATH"] do
# your routes..
end
end
This way you'll have to set the ENV variable:
$ export PROXY_PATH=app2
$ bin/rails s

root path to home/index is not working with unicorn in production?

When I type into my browser my web address www.moneytree.space, I get
How do I make /home/index to be displayed instead. I am using rails app and unicorn and nginx. In my config/routes.rb file the root is set to "/home/index". Thank you.
Just delete index.html in the Rails app's public directory.
When you set the root to "/home/index" in your routes.rb file, you're telling rails that /home/index should route to '/', not that '/' should route to /home/index
You can set '/' to a specific controller action like this:
get '/' => 'sessions#create
so all requests to your main webpage will go the create action of the sessions controller

Trying to visit my website at /get_started redirects to / when I copy and paste, but not when I type it manually

I have a rails app with a page at this url: http://yo4food.com/get_started
You'll notice that clicking the link redirects to http://yo4food.com/
However, if you add a /get_started to the end (type it manually) it does work.
any ideas what's going on?
routes.rb looks like this:
Rails.application.routes.draw do
get 'get_started' => 'pages#get_started'
end
The root route isn't present because it just goes to index.html in the public folder.
http://yo4food.com/get_started is redirecting to http://www.yo4food.com/
http://www.yo4food.com/get_started does not get redirected.

Redirect http://domain.com/subpage to http://www.domain.com/subpage in Rails

I assume this is not really a difficult issue but I am using so many different solutions and don't really know what it best.
I am using Ruby on Rails and have my apps on Heroku and I want to 301 redirect everything on my naked domain (#) to my www-domain. E.g.
http://domain.com --> http://www.domain.com
http://domain.com/subpage --> http://www.domain.com/subpage
Right now I am handling this with my DNS by first deleting both # and www entries. Then I set a redirect of the entire website to http://www.domain.com (which re-creates the DNS entries for both # and www). Lastly I change my www DNS-entry to CNAME and the name of the heroku-app (http://myapp.herokuapp.com).
This seems to be forwarding http://domain.com/subpage to http://www.domain.com (without the subpage).
What I am looking for now is the proper/recommended way to handle this in a simple/elegant way.
DNS?
Routes?
.htaccess? (if so, how do I alter .htaccess in RubyOnRails)
Thanks in advance!
These questions may be related to your question, maybe have a look at those. It seems (from what I have read), that heroku does not allow access to anything like .htaccess or anything, so it seems like Rack middleware would be the best option.
Where is your domain registered? If with GoDaddy, they offer a service that will handle the 301 redirect for you, but it requires that you sign up for one of their hosting plans. The lowest cost will do (~$5 per month). Not free, but painless and requires no coding, etc.
(notice : did not try anything of this, but it should work)
One might argue that the nice way is to use the DNS or your webservers capabilities to do that. However, it is possible to do it with rails if you need it.
All in all, it has the advantage that you will easily keep any params / path in the process, as you told you wanted. Plus, the logic is inside the app and won't be lost if you need to scale up / change domain name. On the bad side, your full stack will be hit anytime someone uses the bare domain name.
you can try a before_filter in your application controller :
before_filter :redirect_to_www
def redirect_to_www
redirect_to subdomain: 'www' unless request.subdomain == 'www'
end
if you want to avoid a "magic redirection" and make it clear for everyone, on rails 3 you can do this directly in the routes:
constraints ->(request){ request.subdomain != 'www' } do
match '(*all)' => redirect( subdomain: 'www' )
end
you should also add the subdomain to your default url options (application controller):
def default_url_options( option = {} )
{subdomain: 'www'}
end
In the end what I had to do was to point the #-DNS to my normal web host (since # demands an IP and Heroku will only accept CNAME) and the www-DNS to Heroku.
At my normal webhost I put a .htaccess that redirected all traffic there to my www-domain like this:
RewriteEngine On
### re-direct to www
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

Resources