Rails 3 Subdomain For Existing Rails Application - ruby-on-rails-3

I was recently tasked with segregating my existing Rails 3 application into a standard and premium version. The premium version will have all the existing functionality with the addtion of a new layout, and new functionality. Ideally I want premuim users to access this site byway of prefixing the current url with a subdomain, e.g, "premium.somesite.com"/ The standard version of the website I don't want to change and is still accessible by the default url.
If possible I would like to simply add some logic to exiting views and controller to determine how the request is treated on whether or not a subdomain is being used .
I tried following the Railcast subdomains in rails 3, however he's using subdomains in context of a blog model and database attributes where as I wanted mine to be application-wide. Is this even possible with subdomains? How can I get started?
What I've tried:
Added an additional VirtualHost to my apache httpd.conf file used by passenger:
<VirtualHost *:80>
ServerName premium.mydomain.com
DocumentRoot /somewhere/../myapp
<Directory /somewhere/public>
Allow from all
Options -Multiviews
</Directory>
</VirtualHost>
Added the following to my routes.rb
class PremiumSubdomain
def self.matches?(request)
request.subdomain == "premium"
end
end
MyApp::Application.routes.draw do
constraints(PremiumSubdomain) do
#all of my routes...
end
end
What happens:
Trying to navigate to my site with the premium subdomain results in a domain not found, adding the constraint to my route.rb prevents the rest of the application from rendering anything on my default url.

So it turned out that while I did have everything configured on apache appropriately we were using a third-party for our DNS server so it was simply a matter of adding a record for our sub-domain and everything else was peachy.

Related

Configuration for two domains on the same server

I have two domains pointed to the same server.
olddomain.com is configured such that DocumentRoot is /var/www/html (Apache server) and finds all HTML and PHP files here. What is the simplest way to have newdomain.com find it's files (different from olddomain.com's) from a different folder (say /var/www/html2), such that when I type:
newdomain.com, it'll show a default index page
newdomain.com/foobar.htm it'll show newdomain.com/foobar.htm (including seeing this in the URL bar)
?
Create virtual hosts for each domain, very easy to do, Google it.

Route not found/matched when using symfony 4 inside a subfolder

I have this apache server configured in mydomain.com/applications and inside that folder I have
/project1/ - using silex
/project2/ - using symfony 4
The server uses mod_rewrite and mydomain.com is represented by /var/www/html which is mapped as DocumentRoot in apache configuration.
In my local environment, /project2/ is mapped as project2.test and routes made with annotations works very well.
However, when moving the project to a directory, its routes doesn't work (whether in production or local env).
I'm pretty sure the routes were created correctly, but I guess I'm missing some symfony configuration when using symfony inside a subfolder.
I also tried to use RewriteBase "/application/project2/" in my /project2/.htaccess but didn't work.
I solved the issue by adding an AliasMatch in my site's conf, inside <VirtualHost> as follows:
AliasMatch "^/project2/(.*)" "/path/to/documentroot/project2/public/$1"
Alternatively I could use just Alias as follows (this was my final solution):
Alias "/project2" "path/to/documentroot/project2/public"
The /project1/ didn't need any change, as .htaccess does the work properly there.
A /project2/public/.htaccess was generated using composer require symfony/apache-pack.

Using fall-through rewrite for mod_vhost_alias

I run a couple dozen sites on my test VPS, and currently use mod_vhost_alias to avoid needing a new VirtualHost every time I throw up a new site. My current configuration looks like this:
<VirtualHost *:80>
ServerName my.servername.com
ServerAlias *
VirtualDocumentRoot /var/www/%0/public_html
</VirtualHost>
Inside my /var/www directory, each site has its own directory. For instance, the path to my personal page is /var/www/personalsite.com/public_html/index.php. This is working great for requests to http://personalsite.com.
However, this does not work when requests come in for http://www.personalsite.com. For some of my other sites, I have the inverse problem -- the directory may be /var/www/www.sitename.com/public_html, so requests for http://www.sitename.com are fine. However, requests for http://sitename.com do not work.
Is there a way to set up my Apache config so that when a request comes in, it does the following? Are there any performance implications of doing it this way?
In pseudocode:
1. Check if the directory or file exists. If it does, skip the rest of the rules
(but don't stop, in case a local .htaccess has rules in it for pretty URLs
in WordPress or Concrete5)
2. If the file/directory does not exist:
1. If the host header starts with "www":
1. remove the www from the host header and try the first rule again.
2. If the host header does not start with "www":
1. add "www" to the beginning of the host header and try the first rule again
3. If it still fails after trying both conditions:
1. Go to a 404 error page
I'm currently doing this with about 20 virtualhosts, but that seems ridiculous when I have to add a new one for each site. The point of using mod_vhost_alias was to avoid needing all these VirtualHosts in the first place.
Assuming you're OK with redirecting users, you can use one of the techniques from https://stackoverflow.com/a/2361508/881615:
either set a mod_rewrite rule to remove the leading www. from requests,
or set a redirect in each vhost to redirect requests with a leading www. to the top-level domain

Zend sub-folder as DocumentRoot for sub-domain gives invalid controller thinking folder name is a controller

I have a zend project located in the /account sub folder. I want to be able to create sub-domain virtual hosts and point their DocumentRoot to this sub folder
In index.php I have this line that sets the URL of the zend project folder
define('SiteUrlUser','http://'.$_SERVER['HTTP_HOST'].'/account/');
The DocumentRoot setup for one sub-domain is
DocumentRoot /var/www/account
As i'm browsing the site at subomdain.domain.com it always adds /account to the URLs which is normal but then throws this error
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (account)'
Now if I remove /account from any URL while accessing the site through a subdomain the pages render just fine. So basically www.domain.com/account/something.html in a subdomain would be subdomain.domain.com/something.html which is working perfectly fine.
I'm planning to setup the server so that all users will login through their sub-domain. The zend folder will be put on it's own server and used as the DocumentRoot for all sub-domains so what would I change in the index.php to make this work?
Thanks
I wouldnt nest the project inside another non-zend project, i would give it its own root out side the web accessible dir of the other project and point the subdomain there. Then just make your module/controller for account the default via routing.
However if you do nest them then you will have to set the baseUrl
If the parent project is also a zend app then i would merge the two together and use routing to handle the sub-dir mapping this way you can do a wildcard vhost and point them all to the same application instance on the filesystem.

How to setup sub-domains like blogspot

What should do to setup a sub-domain for the users when they sign-up into my site.
What are the infrastructure required? I am using Linux servers.
You can either use a specific DNS (CNAME or A/AAAA) entry for each known subdomain, or a wild-card DNS entry that'll accept *.example.com:
$ORIGIN example.com
foo IN A 12.34.6.78
bar IN A 12.34.6.78
or
$ORIGIN example.com
* IN A 12.34.6.78
The advantage of this latter is that no changes are required to either DNS or Apache configuration once the service is running. The disadvantage is that all such wildcard lookups must (by definition) end up returning the same IP address.
The Apache configuration will depend on your requirements, both for end-user control and security. Note that if the users have permission to run CGI scripts on the server then additional setup will be needed to ensure that that's done securely.
Depending on whether content is static or dynamic this will also affect your configuration:
Use mod_vhost_alias to map individual virtual hosts into their individual directories on the server.
If you really want, create a separate <VirtualHost> section for each known site, but then you'll have to restart Apache each time a new user signs up
Use a single <VirtualHost> and then look at the hostname part of the requested URL (from the $SERVER_NAME environment variable) in the scripts that render the output to figure out which user's content to display.
You can make a CNAME entry/ A Record in your DNS settings, for each subdomain
A CNAME record is a record in your
Domain Management Settings that allows
you to control a subdomain of your
domain.
To automate it along with registration, you can write a script which is executed for each user, when s/he registers.
You can refer to this link, as well, for a step-by-step process for Apache:
How to setup subdomains in apache
(since you mentioned Linux, I assume it must be APache. Please mention if it is otherwise)
Alternate Solution
You can also refer to the wildcard solution, given by Alnitak, in the same thread. I find his is an easier way. :)
infrastructure includes access the the dns server to add a wildcard entry, and rewrite rules in Apache.
Try these answers:
How to let PHP to create subdomain automatically for each user?
How to make subdomain user accounts in a webapp
or this link:
http://jam.jrox.com/docs/index.php?article=76
If your using Linux server's I'm assuming your using Apache as your webserver.
You'll have to setup proper DNS routing for the sub domain as well as a virtual host.
Virtual Hosts are fairly easy to setup but I'm not sure how easy it is to do them on the fly progmatically.
Most of the time it's as easy as editing your apache config file and adding the following:
Port 80
ServerName www.mydomain.com
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/user-bob
ServerName bob.mydomain.com
...
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/user-sally
ServerName sally.mydomain.com
...
</VirtualHost>
The VirtualHost Documention will probably of some use to you.
Apache allows you to specify any number of 'sites' based on subdomains on a single server. Creating a new 'site definition' file with the appropriate subdomain information in it, along with proper DNS wildcards, will do what you want.
In other words, the process is like this:
Setup wildcards so that *.mysite.com directs to the proper server.
When a new user signs up, create the proper Apache site definition file - you'll probably have a base template that you put the right subdomain information into and save.
Make Apache re-read its configuration.
Profit.
IMPORTANT This is based on a Debian-style Apache configuration, where the config files are included in a directory, and the main configuration reads all the config files in that directory. This will simplify things a great deal, because adding/removing subdomains will mean adding/removing files, rather than editing a single file, and so the process will be much easier to automate.