How can I achieve different URL start in the same domain? - url

Let's suppose I develop a website, let's suppose it has a domain of foo.bar.
Let's suppose further that I have a user called lorem and a user called ipsum. I would like to create an URL of lorem.foo.bar for lorem and a URL of ipsum.foo.bar for ipsum. What are the possible technologies with which I can achieve that? Can you guys give any example in any technology where this is achieved?
Thanks so much.

Depending on your needs I find this approach to be the most straightforward.
In my experience the best way to do what you are trying to do is through Wildcard domains which is configured at your DNS server. In your example *.foo.bar would be the wildcard for your foo.bar domain. Typically these are setup on A records but can be setup on MX records and the like. The only real requirement here is that your DNS server supports wildcard domains.
As far as the Webserver goes, you will need to employ some sort of URL Rewriting technique so that lorem.foo.bar displays the lorem page and so on. There are several techniques to ReWrite URLs. On Apache you can configure VirtualHosts or use mod_rewrite. On IIS there are, again, several methods using either ISAPI filters or you can use URL Routes in ASP.NET MVC to do the rewriting.
Here are some relevant examples:
Creating Wildcard Sub Domain Using Apache VirtualHost
URL rewriting in .NET 4?
http://support.microsoft.com/kb/840687
http://www.isapirewrite.com/docs/
-Edit per comment
In your example of *.foo.bar you will only ever setup a single wildcard domain on your DNS server. Once that is done you shouldn't ever have to do it again unless you add different wild cards.
As far as Apache goes you should be able to do something like this (I haven't used Apache in a long time so this might be slightly inaccurate)
<VirtualHost 0.0.0.0>
DocumentRoot /www/[directory sub domains are served out of]
ServerName www.foo.bar
ServerAlias *.foo.bar
</VirtualHost>
Again this should be a one time configuration or as you add more wildcards. From here your PHP (or whatever language you are using) would need to handle the actual processing of what content it should display based on the given subdomain.
--edit 2 for second comment
So a wildcard domain allows for pretty much any value. In the above case *.foo.bar would allow for lorem.foo.bar, ipsum.foo.bar, someguy.foo.bar, and so on without requiring you to add additional entries to your DNS server or add additional VirtualHost entries. So using the VirtualHost configuration above lets say the DocumentRoot was /www/subdomains. When navigating to lorem.foo.bar or ipsum.foo.bar both requests get served out of the /www/subdomains folder so the code in there would need to grab the name of the subdomain (lorem or ipsum in this case) and then act on those values by querying the database or whatever other business processes you have in place. Attempting to create a single directory , DNS entry, and/or VirtualHost entry for this process is a mistake because it will become an absolute management nightmare. In the example above you only ever have to worry about 1 DNS entry, 1 VirtualHost entry, and one directory worth images, web pages, PHP scripts, etc.

Related

Route 53 - Special domain for a single page on existing server

I have a complex web app at example-app.com, hosting fully on AWS using ELB and Route 53 for DNS. It's a Rails app.
I'm running an experiment that I'm using in the rails app, at example-app.com/test. I want to set up new-domain-app.com, to point at example-app.com/test, and have the URL cloacked to always be new-domain-app.com. It's a single page site, so it shouldn't require any navigation.
I'm having a lot of trouble figuring out how to set up my DNS on Route 53 to accomplish this. Does anyone have good ideas on what this Route 53 configuration should look like?
AWS offers a very simple way to implement this -- with CloudFront. Forget about the fact that it's marketed as a CDN. It's also a reverse proxy that can prepend a fixed value onto the path, and send a different hostname to the back-end server than the one typed into the browser, which sounds like what you need.
Create a CloudFront web distribution.
Configure the new domain name as an alternate domain name for the distribution.
For the origin server, put your existing hostname.
For the origin path, put /test -- or whatever string you want prefixed onto the path sent by the browser.
Configure the cache behavior as needed -- enable forwarding of the query string or cookies if needed and any headers your app wants to see, but not Host.
Point your new domain name at CloudFront... But before you do that, note that your CloudFront distribution has a dxxxexample.cloudfront.net hostname. After the distribution finishes setting up (the "In Progress" status goes away, usually in 5 to 20 minutes) your site should be accessible at the cloudfront.net hostname.
How this works: When you type http://example.com into the browser, CloudFront will add the origin path onto the path the browser sends, so GET / HTTP/1.1 becomes GET /test/ HTTP/1.1. This configuration just prefixes every request's path with the string you specified as the origin path, and sends it on to the server. The browser address bar does not change, because this is not a redirect. The host header sent by the browser is replaced with the hostname of the origin server when the request is sent to the origin.
What you are trying to do is not possible. Route53 is a DNS system, and you can not configure a hostname (e.g. new-domain-app.com) to point to URL (e.g. http://example-app.com/test) using DNS.
However, you are probably using a wrong tool for the job. If example-app.com/test is indeed a simple, static, single page site, then you do not need to host it inside Rails app. Instead, you can host it on AWS S3 bucket, and then you can point new-domain-app.com to that bucket using Route53.
See the following for details:
http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html
http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/RoutingToS3Bucket.html
DNS knows about Domains, not url's. DNS simply converts names to IP addresses.
You can't do what you are asking for just using DNS and ELB, however, what you can do is have a seperate VHOST for new-domain-app.com that points to your example-app.com site and accomplishes what you want using some sort of redirection rule that only fires for new-domain-app.com.
I'm not sure that this qualifies as an SO question, and more likely is a serverfault question. Specifics about your webserver and OS platform would be helpful in getting more specific advice.
So here's some details:
You already have example-app.com setup and working
You create a CNAME entry pointing new-domain-app.com to example-app.com or you can make an A record pointing to the same IP. If you already have example-app.com pointing to a different IP address, then use a subdomain (test.example-app.com) to isolate it.
Setup a new vhost on your server that basically duplicates the existing vhost for new-domain-app.com. The only thing you need to change is the server name configuration.
Why does this work? Because HTTP 1.1 included the HOST header that browsers send along, and web servers use in vhosting to determine which virtual host to route an incoming request to. When it sees that the client browser wanted "example-app.com" it routes the request to the appropriate vhost.
Rather than having to do some fancy proxying, which certainly can be used to get to a similar result, you can just add a redirection rule that looks for requests for the host example-app.com and redirects those to example-app.com. In apache that uses mod_rewrite which people often utilize by putting rules in the ubiquitous .htacess file, but can also be done in nginx and other common web servers. The specifics are slightly different for each.

rewrite url's but for a different domain

We're in a need to rewrite some url's in our site (we use NGINX) so they will appear as a different domain.
For example we want www.mydomain.com/category to become www.newdomain.com
Also any related Url, should follow the same path
For example,
www.mydomain.com/category/news to become www.newdomain.com/news
Is this possible from server side and if yes, how can we do it ?
Important Notice: The mydomain.com/category is a "virtual" folder, I mean that there is no directory in the server with files but it's being created automatically from the script.
Best Regards

Trying to shorten over 90 urls over several folders

I have over 90 urls set out in the following format:
http://www.mysite.com/folder1/folder2/page.html
Each of these URLs will be printed on paper for a user to input into their address bar. The problem at the moment is they are too long and therefore I need make these URLs as short as possible.
However, what would be the best method for doing so?
Would sub folders be the best thing here, such as "keyword.mysite.com"?
I don't want to use a url shortening service as they still need to be related to my domain name. Additional domain names forwarding on to the pages are also out of the question due to the quantity of urls.
Richard
Without knowing what technology you are working with (apache/php, asp.net, JSP, etc) all I can suggest is investigating Url Rewriting. Here is a codeproject example of a rewriter for ASP.Net.
There's a handful of mechanisms that come to mind quickly. One is to host your own url-shortening service for your own domain: http://docs.example.com/xsdf and so forth. Writing one for your own users shouldn't be too much work, especially since you could even write a quick script to submit all the URLs for shortening and replace them all without ever making a pretty interface for a human.
If you want something even cheaper, but more work on the part of your server admins, you could use the standard 'rewriting' services in web servers:
Apache mod_rewrite guide
RewriteRule ^/xsdf$ folder1/folder2/page.html [R]
RewriteRule ^/qwer$ folder2/folder3/page.html [R]
RewriteRule ^/polz$ folder7/folder6/page.html [R]
nginx HttpRewriteModule.
rewrite ^/xsdf$ folder1/folder2/page.html redirect;
rewrite ^/qwer$ folder2/folder3/page.html redirect;
rewrite ^/polz$ folder7/folder6/page.html redirect;
Updating these rewrite rules involves editing the server config files, or dropping new ones in place. The other mechanism would be outside the range of the web server itself, so it might be easier or harder for long-term maintenance depending upon which your team would rather work with in the future.

How do I serve multiple rails apps on one domain (and sub)?

This is kind of weird but I'd like to serve multiple websites on the same domain. If possible, we want to avoid subdomains to keep urls simple for our users - no need for them to know it's two separate apps. This is purely for keeping the code bases separate. Any ideas?
For example:
Rails App 1 (Refinery CMS) serves:
http://example.com/
http://example.com/about
http://example.com/pricing
Rails App 2 (our real App) serves:
http://example.com/account
http://example.com/store
http://example.com/listings
We use ruby 1.9.2, ruby on rails, refinery cms, apache and passenger.
If you're using Passenger, check out the Deploying to a sub URI portion of the manual - it's quite simple to set up an app on a sub-URI. You may need to set config.action_controller.relative_url_root in your app configuration, as well.
Edit: I misread the question; not one app per URI, but one app serving some (but not all) endpoints. This is actually moderately easy to do with some basic rewrites, as well.
Deploy your Rails app to, let's say, /railsapp (but without setting relative_url_root). Now, in .htaccess:
RewriteRule ^account/(.*)$ railsapp/account/$1 [L]
This will internally remap /account/* to /railsapp/account/*, so as long as you set up a rewrite per path your Rails app handles, it should work fine.
Subdomains make it easier (thus why most sites have shop.example.com), but you could probably use rewrite rules with name based virtual host routing. How exactly to do that I'm not sure. More of a Apache rewrite question for SuperUser.
A word of warning if you are using SSL you might have issues arise.
You could set it up to first hit one app where you expect most URLs would work and if it 404s you could instruct it to try the other app next, though this will be slower than routing per route but it will work without having to hardcode a route for every page that is created on say, Refinery CMS.
Currently I'm also working on a same kind of CMS. In my case also I need multiple sub domains, like
www.test1.mydomain.com
www.test2.mydomain.com
www.test3.mydomain.com
www.test4.mydomain.com
here is what I did
in rails 3 (if you are on rails3) you can get the sub domain by using request object. (If you are on rails 2.x you can use sub domain_fu plugin)
In my case I have used a before filter to get the sub domain, after that I load the site according to the sub domain
For development use the following public domain "lvh.me"
http://tbaggery.com/2010/03/04/smack-a-ho-st.html
this was very useful for me http://railscasts.com/episodes/221-subdomains-in-rails-3
let users have their domains forwarded to your subdomain (with masking)
ex : www.clientdomain.com --> http://client.mydomain.com
hope this helps
cheers
sameera

Mapping multiple domain names to different resources in a Rails app

The rails app I have allows users to manage holiday homes. Each property has it's own
"website/homepage" within my app and a user can tweak the content, it works well,
quite pleased so far. Typical rails approach to the resources so the URLs to a particular property look like this for the "homepage" of a particular property.
localhost:3000/properties/1/
then
localhost:3000/properties/1/full_details
localhost:3000/properties/1/price_list
etc
Requirement is to map a domain name e.g. www.chalet-yeti.com and have it resolve (rewrite?) to localhost:3000/properties/1/
like so also...
www.chalet-yeti.com/full_details -> localhost:3000/properties/1/full_details
The next user adds a property and I register a new name on their behalf and I'd like to do this of course..
www.apartment-marie.com -> localhost:3000/properties/2/
Is this possible/advisable/doable in the same rails app? So far solutions have ranged from "why would you do that" to variations on "use mod_proxy / mod_rewrite / virtual_host config". In case it matters the app runs under apache and passenger on my server.
I don't want to pre-empt an answer but most people so far seem to point to apache configuration and most say what I'm attempting is not impossible / inadvisable. Really hope someone could at least point me in the right direction as I've been head scratching all morning. Out of my comfort zone here and I'm hoping I can launch my app and haven't spent six weeks building a white elephant! Unless I can do this URL thing, it's dead!
http://37signals.com/svn/posts/1512-how-to-do-basecamp-style-subdomains-in-rails
This is what you want. Don't mess with apache for that. It doesn't scale to hundreds of domains and it's prone to breakage.
Within Rails, you should think of the requests coming just to a URI, without a host name section. That is, instead of localhost:3000/properties/1/full_details you need to think of /properties/1/full_details. The localhost:3000 part is just to get the request to Mongrel during the development process.
So what you really want is to take the request as it is received by the HTTPd (Apache, in your case) and extract some information to construct the request which is given to Rails.
mod_rewrite, which is an Apache module, is the sane way to do this.
You need to ensure that the same virtual host which runs your Rails application accepts requests for all the domain names you're using.
Then you can use mod_rewrite to do something like this:
RewriteCond %{HTTP_HOST} ^(www.)?chalet-yeti\.com$
RewriteRule ^(.*)$ /properties/1/$1 [L]
This will take every request to the host chalet-yeti.com (or www.chalet-yeti.com) and hand them to Rails as "/properties/1/$1" (where the $1 is any additional path, like full_details).
You'll need a block like that for each of your domains, but that's just two lines in your Apache configuration. Unless you're doing hundreds of domains, it should be tolerable, right?

Resources