Help needed with virtual sub domains and some other bits.

New to mod_rewrite? This is a good place to start.

Help needed with virtual sub domains and some other bits.

Postby andrewpmoore » Thu Nov 08, 2007 3:04 am

Hi,
I'm a developer writing a dynamic web application that at the moment has page urls like the following:

http://www.example.com/thepencilroom/home/website/test
or
http://www.example.com/thepencilroom/pa ... t/pageId/3
or
http://www.example.com/thepencilroom/di ... bsite/test

thepencilroom is the application name
page or diary is the servlet that serves the page
and then all the other parameters are name and value pairs (ie pageId = 3)

I'm a complete n00b when it comes to apache, but what I'm wanting is that if a user enters the url
test.example.com it would redirect to
www.example.com/thepencilroom/home/website/test (the home page)

or
anothertest.example.com would redirect to
www.example.com/thepencilroom/home/website/anothertest

For all other entries for example
test.example.com/thepencilroom/page/pageId3 would redirect to
http://www.example.com/thepencilroom/pa ... t/pageId/3 (or whatever page was entered)


Hope that makes sense.

I've got access to the server as it will be self hosted so setting parameters in apache.conf won't be a problem.


Thanks for looking
Andrew
andrewpmoore
 
Posts: 9
Joined: Mon Oct 29, 2007 7:41 am

Postby richardk » Fri Nov 09, 2007 3:21 pm

You will need DNS for the sub domains. Then in your apache.conf you should have a <VirtualHost> for example.com. It needs to include
Code: Select all
<VirtualHost *:80>
  ServerName example.com
  ServerAlias *.example.com

  Options +FollowSymLinks

  RewriteEngine On

  RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
  RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
  RewriteRule ^(/.*)$ /thepencilroom/home/website/%2$1 [QSA,PT,L]
</VirtualHost>

Then restart Apache.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby andrewpmoore » Thu Nov 15, 2007 1:44 am

Thanks. After lots of problems getting any of this working and finding out it was an issue with how apache was forwarding it's requests onto the tomcat application server, I'm now a little further on.

I now currently have:
Code: Select all
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^(/.*)$ /thepencilroom%{REQUEST_URI}/website/%2 [QSA,PT,L]


This is correctly appending the details that I need.

The only thing I've got left to sort out is that at the moment when a user enters for example
http://testb.domain.com or http://testb.domain.com/
I want them to automatically redirect to:
http://testb.domain.com/home
before processing the other condition.

I'm just not sure how to test that the user hasn't entered anything after the domain name.

Thanks
Andrew
andrewpmoore
 
Posts: 9
Joined: Mon Oct 29, 2007 7:41 am

Postby richardk » Thu Nov 15, 2007 4:53 pm

I'm just not sure how to test that the user hasn't entered anything after the domain name.

In <VirtualHost>s match /
Code: Select all
RewriteRule ^/$ /home [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby andrewpmoore » Wed Nov 21, 2007 1:26 am

Thanks again, that's working great now.

There's now only one thing left to do as far as url's are concerned anyway.

I now have working that a user registers to use the system and gets their own sub domain automatically.

So user fred registers and gets
fred.example.com

What I'm wanting also is that if fred has registered www.fred.com that that maps to fred.example.com

I know it's not exactly a mod rewrite question, but you guys are probably familiar with this sort of stuff.

I thought I could do this with a CNAME record in the dns for www.fred.com pointing to fred.example.com. but when I try that, it only ever goes to www.example.com

So does anyone know how to set up the scenario above?
Thanks again.
andrewpmoore
 
Posts: 9
Joined: Mon Oct 29, 2007 7:41 am

Postby richardk » Wed Nov 21, 2007 2:58 pm

I thought I could do this with a CNAME record in the dns for www.fred.com pointing to fred.example.com.

It doesn't have to be for their sub domain, just any domain that goes to your server's IP address.

but when I try that, it only ever goes to www.example.com

It won't match the mod_rewrite because the HTTP_HOST variable will be set to their domain.

You could find their username by setting up a text file RewriteMap, eg. (in /path/to/domain-username.map)
Code: Select all
fred.com fred
bob2.com bob
zxy.net dave

and in your <VirtualHost>
Code: Select all
<VirtualHost *:80>
  ServerName example.com
  ServerAlias *.example.com

  Options +FollowSymLinks

  RewriteEngine On

  RewriteMap domain2username txt:/path/to/domain-username.map

  RewriteRule ^/$ /home [R=301,L]

  RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
  RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
  RewriteRule ^(/.*)$ /thepencilroom/home/website/%2$1 [QSA,PT,L]

  RewriteCond %{HTTP_HOST} !^(.*\.)?example\.com$ [NC]
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteCond ${domain2username:%1|:} ^([^:]+)$
  RewriteRule ^(/.*)$ /thepencilroom/home/website/%1$1 [QSA,PT,L]
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby andrewpmoore » Thu Nov 22, 2007 1:26 am

Thanks again. That makes sense.
Just one question from that.

Would the app server need to be bounced to pick up on the changes in the external file, or would the file get read on each request?, as I'm looking for a way to do this without any user intervention (ie the mapping file would be maintained by the front-end)
andrewpmoore
 
Posts: 9
Joined: Mon Oct 29, 2007 7:41 am

Postby richardk » Thu Nov 22, 2007 1:17 pm

I'm not quite sure what you mean by bounced, the docs say
For plain text and DBM format files the looked-up keys are cached in-core until the mtime of the mapfile changes or the server does a restart.


There are also other types of RewriteMap.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby andrewpmoore » Thu Nov 22, 2007 10:21 pm

When I say bounced, I mean, stopped and then restarted.
But the quote you extracted from the apache docs answers my question.

Thanks once more! :D
andrewpmoore
 
Posts: 9
Joined: Mon Oct 29, 2007 7:41 am

Postby andrewpmoore » Fri Nov 23, 2007 12:44 am

Well, after several months of wondering how I was going to do this, and then finding this forum I've finally got the configuration that I was wanting.

Huge thanks to richardk for all your help, I'd have never got this sorted without you.

Just in case anybody is interested, here is my final output:
Code: Select all
RewriteEngine On
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 9

RewriteMap domain2website txt:/etc/apache2/domain-websitemap.map

#don't rewrite these references, leave them as is
RewriteRule ^/static(/.*)?$ - [NC,L]
RewriteRule ^/thepencilroom/app(/.*)?$ - [NC,L]
RewriteRule ^/thepencilroom/app(/.*)?$ - [NC,L]
RewriteRule ^/thepencilroom/response(/.*)?$ - [NC,L]
RewriteRule ^/thepencilroom/contact(/.*)?$ - [NC,L]
RewriteRule ^/thepencilroom/cart(/.*)?$ - [NC,L]


#if the url is blank other than the domain, add "home" to it and continue
RewriteCond %{SCRIPT_FILENAME} ^/$
RewriteRule ^(/.*)$ /home [R]

#to allow the system to work without apache (ie, just using tomcat) most urls written will put in "thepencilroom"
#to get to the servlet, but because when using apache "thepencilroom" is automatically added
#if the incoming url contains the text, then remove it
RewriteCond %{SCRIPT_FILENAME} ^/thepencilroom/(.*)$
RewriteRule ^(/.*)$ /%1 [R]

#quick access to the admin console via admin.example.com
RewriteCond %{HTTP_HOST} ^admin\.example\.com$ [NC]
RewriteRule ^(/.*)$ /thepencilroom/app [QSA,PT,L]

#do the virutal mass hosting
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(/.*)$ /thepencilroom%{REQUEST_URI}/website/%2 [QSA,PT,L]

#do the mapping from a real registered domain to the appropriate example website
RewriteCond %{HTTP_HOST} !^(.*\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond ${domain2website:%1|:} ^([^:]+)$
RewriteRule ^(/.*)$ /thepencilroom%{REQUEST_URI}/website/%1 [QSA,PT,L]
andrewpmoore
 
Posts: 9
Joined: Mon Oct 29, 2007 7:41 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 34 guests

cron