Forum Moderators: phranque

Message Too Old, No Replies

Rails and mod rewrite

         

oswald_yang

12:43 pm on Apr 21, 2011 (gmt 0)

10+ Year Member



Hi,

We've recently built a CMS in (Ruby on) Rails that will host many a site, the sites can currently be accessed as follows:

example.com/site1/home
example.com/site2/home
example.com/site3/home
etc...

I'll create an Apache vhost per site, for instance 'siteone.com' (example.com/site1/home). I pointed it to the 'public' folder of the Rails app and it uses the following rewrite rule to access the correct path:

RewriteRule ^/$ http://siteone.com/siteone/home


Thing is I'd like it to access 'http://siteone.com/siteone/home', but only display 'http://siteone.com/'. Note that '^/siteone/home' doesn't physically exist. I've tried a few rules but aren't sure how I should go about this.

Any advice/assistance would be much appreciated.

Thanks,
Charl

g1smd

6:02 pm on Apr 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The code above is for a 302 external redirect to a new URL. The solution you require is an internal rewrite based on the domain name in the HTTP request.

As such you'll need at least a RewriteCond looking at the HTTP_HOST variable.

What other rules did you try?

What was the result?

How did that differ from what you expected?

"Not working" is too vague a description to be of any help in assessing the problem and the required solution.

You need to be clear that URLs are a reference system used out on the web and that files and paths are a separate reference system used inside the server. It is very important to understand that mod_rewrite is used merely to map URL requests to the internal server filesystem, and that it can do so using any scheme that you desire.

jdMorgan

6:26 pm on Apr 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If /siteone/home does not physically exist, then how do you produce the content for that path? Do you use another rewriterule (not described) or an Alias directive to map it to Rails, or what?

For you main question, it appears that you will need to set up wild-card dns on a server with a unique IP address, and then map requested hostnames to either the correct subdirectory path or to a correctly-formatted call to Rails. However, the information in this thread is not sufficient to determine the correct solution.

Jim

oswald_yang

9:52 am on May 10, 2011 (gmt 0)

10+ Year Member



Hi,

Just an update (and thanks for getting back to me). I've been on a two week holiday and got cursed with a horrible flu SO only got back in the office yesterday.

I did manage to sort this however. So to explain the situation - which I failed to do thoroughly first time round.

The application is written in Ruby on Rails (which has it's own server named WEBrick that serves the app on port 3000 by default) and is being server by Apache via a mod named Phusion Passenger. Passenger makes serving Rails apps via Apache much less of an hassle than many other solutions.

Now Rails has internal 'routes' that defines the paths, for instance:

example.com/site1/home
example.com/site2/search/listings/4572346


So as soon as example.com gets hit the content gets served by WEBrick via Apache and everything after example.com (for instance ^/site1/home) is not physical, neither a regular query string, but a Rails route, hence my confusion on how to handle it with mod_rewrite.

The odd thing is I ask a few times on the rails and passenger IRC channels but no one seemed to know the answer.

So what one would usually do to have:

http://siteone.com/siteone/home


display as:

http://siteone.com/


would be:

RewriteRule ^/$ /siteone/home [L]


Which gave me a server error. So what worked, and I should have thought if it immediately, was to add the proxy flag and have mod_proxy handle it :)

RewriteRule ^/$ /siteone/home [P,L]


I hope this can help someone with the same question in the future.

Cheers,
Charl

oswald_yang

10:09 am on May 10, 2011 (gmt 0)

10+ Year Member



UPDATE: Note the the [PT] flag also works and is preferable.