Sinatra Static Page - ruby

In my public folder, I have a file index.html. I want to be able to just use the url localhost:4567 to display the page (without displaying localhost:4567/index.html)
This is my current ruby script:
require 'sinatra'
set :public_folder, 'public'
get '/' do
redirect '/index.html'
end
I have tried removing the redirect statement, but the url still comes up with the index.html.

You could use send_file here:
get "/" do
send_file 'public/index.html'
end
You need to provide the full path of the file from the working directory (i.e. not just the path under public), and this only works at the root url, it won’t serve index.html pages for directories generally. If you want that you would probably need to set up a separate web server in front of Sinatra and configure it appropriately.

The idea behind redirects is exactly to force the browser make a new request to where you are redirecting to.
As you don't want to change the url, you have two possible solutions:
Rewrite instead of redirect. Sinatra itself doesn't provide such functionality, but you can easily use a rack middleware:
require 'rack/rewrite'
use Rack::Rewrite do
rewrite '/', '/index.html'
end
Serve the index.html contents when asked for the root path:
get '/' do
File.read("#{APP_ROOT}/public/index.html")
end

Related

Generate route url from subdomain to the main domain

I have a multi-tenant app. So let's say I have test.app.com and app.com. I can successfully navigate from app.com to test.app.com but I am unable to the opposite.
I tried wrapping the routes of my app.com inside a Route::domain() but that only gives me a 404 error instead of actually redirecting it to the url
EDIT: Just to make sure, I want to redirect using a relative path (with the route() method).

How do I set a redirect for my root index.html in jekyll?

I have jekyll-redirect-from plugin, but it only really works for pages that aren't my root homepage.
For example, if a user types in www.mywebsite.com/index.html I want it to redirect and display the URL as www.mywebsite.com
Everything I can find about this is focused on blog posts and other pages than the index. Has anyone had this issue?
You don't need a redirect, because www.mywebsite.com/index.html and www.mywebsite.com are the same page. (The browser just shows the index file by default if you go to www.mywebsite.com.)
You need the browser to rewrite the URL, while remaining on the same page. You need to do this on the webserver.
.htaccess
If your site is on an Apache server and you have access to the server, you can use an .htaccess file to rewrite the URL from www.mywebsite.com/index.html to www.mywebsite.com.
There are online .htaccess generators like this one that help get the syntax right.
There are similar methods for rewriting URLs on nginx webservers.
GitHub Pages
If you're on a service like GitHub Pages, you can't use .htaccess. A free workaround is to use Netlify to deploy your site, because you can set up redirects on Netlify. Create a free account on Netlify and add a new site from GitHub there.
In the root of your repo, create a netlify.toml file containing this:
# Redirect /index.html to /
[[redirects]]
from = "/index.html"
to = "/"
Netlify will now handle that redirect.

redirect to a subpage as the homepage in a fuel CMS website

I'm using fuel CMS for the first time -- I'm wondering how to set a redirect in the /index.php so when you navigate to the website base URL, your redirect to a /subpage/ the subpage will always act as home.
Note: Normal PHP redirects and meta html redirects DO NOT WORK. I just get 'redirect loop error' I guess due to the architecture of the CMS modules etc. Something like even after redirected, files from index.php and header.php are re-read so you get caught in a redirect.
Anyone familiar with the framework, any pointers would be awesome, thanks!
I think I got it!
(for those who care.. or for more importantly, to the masses who do not!)
$config['aggressive_redirects'] = array(
'/' => array('/solutions', TRUE, 302)
);
Via documentation.. http://docs.getfuelcms.com/general/redirects let's see how this performs.

Is it possible to serve static HTML without the file extension?

I have a ruby + sinatra website where most of the files are static HTML, meaning they can be accessed by going to http://www.mydomain.com/features.html, http://www.mydomain.com/pricing.html, etc... (I am using sinatra for a few more dynamic pages)
What I would like to do is serve these static HTML files without their file extensions. So going to http://www.mydomain.com/features would show the same as if I currently visited http://www.mydomain.com/features.html currently.
I have tried simply removing my .html file extensions, however when trying to open these files in the browser, the browser downloads downloads the file instead of rendering it on screen.
Is it possible to continue serving my static .html files just without their file extensions? Thanks!
If you do this:
require 'sinatra'
get '/:file_name' do |fname|
send_file(
File.join(settings.public_folder, "#{fname}.html")
)
end
...then either url:
http://www.mydomain.com/features.html
http://www.mydomain.com/features
will return the same page.
settings.public_folder: By default, this is assumed to be a directory
named “public” within the root directory...http://www.sinatrarb.com/configuration.html
If you define the routes for your dynamic pages above that route, then they will match first.

URL Rewrite to remove my controller's name from the url displayed

I have this site
http://cjbuilders.info/welcome/home
and all the links start with
http://cjbuilders.info/welcome
How can I use mod_rewrite to just remove
/welcome/
from the url? This should be an easy one, but i struggle with mod_rewrite.
Do you know about CodeIgniter's URI Routing? Add this into your routes.php config file and it should work just fine:
$route['home'] = 'welcome/home';
This should work, IIRC:
RewriteRule ^/welcome/(.*)$ /$1 [R]
However, guessing a bit about what's going on here, if the reason for this prefix is something like a Java app server deploying an app at a context called "welcome", then the better solution is not to rewrite the URLs but to fix the backend app server to have a null context, i.e. serve at / rather than at /welcome/.
This is because the app server will probably want to generate links to other views of its app, and will reinsert the "welcome": this becomes a pain, and means that all links on your pages will get HTTP redirects when visited (e.g. by search engines). There is no way that the proxying apache server can parse the HTML and tell when that "welcome" should be removed, so best to fix the server that's writing the links in the first place.

Resources