Welcome to WebmasterWorld Guest from 35.153.135.60

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

RewriteRule needed

converts mail.mydomain.com to point to a full path using proxy

     
12:26 am on Dec 31, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 12, 2003
posts:51
votes: 0


Here's what I've got so far...


RewriteEngine on
RewriteCond %{HTTP_HOST} ^mail\.mydomain\.net$
RewriteRule ^/$ http://www.anotherdomain.com/webmail/ [P,L]

This converts mail.mydomain.net to points it to anotherdomain.com/webmail. No problem. Notice that I'm using the P at the end because I still want mail.mydomain.net to show up as the URL. This also works fine, but the problem is, when the images load, they are trying to load from mydomain.net and not from aotherdomain.com, as they should. So I end up with images (and externally referenced style sheets and JS) do not appear. How can I correct this?

Secondly, I want to do this across the board for all of my domains listed in httpd.conf. I have tried moving these directives near the top of the config file, but they don't seem to work unless I have them shown in one of the virtual host blocks. I'd rather not do this. I would rather have one declaration near the top somewhere instead of making a seperate rewrite rule everytime I add another vhost. Any suggestions on what to change so they work outside the vhost blocks?

If I need to post my httpd.conf, will I need to remove all of the URL's to comply with site policy?

12:50 am on Dec 31, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Donboy,

This will redirect *all* requests to mail.mydomain.net to the same pages/images/files on www.anotherdomain.com/webmail :


RewriteEngine on
RewriteCond %{HTTP_HOST} ^mail\.mydomain\.net
RewriteRule ^/[b](.*)[/b] http://www.anotherdomain.com/webmail[b]/$1[/b] [P,L]

ref: [httpd.apache.org...]

Secondly, I want to do this across the board for all of my domains listed in httpd.conf. I have tried moving these directives near the top of the config file, but they don't seem to work unless I have them shown in one of the virtual host blocks. I'd rather not do this. I would rather have one declaration near the top somewhere instead of making a seperate rewrite rule everytime I add another vhost. Any suggestions on what to change so they work outside the vhost blocks?

Not sure, but someone else will be!

If I need to post my httpd.conf, will I need to remove all of the URL's to comply with site policy?

Yes, please use "example.com" or "example.net", and trim it down as far as possible (e.g. you don't need to show *all* of your vhosts) to reduce its size. The purpose of our TOS and forum charter is to keep the discussion useful to many people, rather than simply being a "repair shop", and also to prevent URL-dropping, which quickly jams a forum with useless self-promotional posts. Exceptions are made for globally-recognized entities like google, and non-promotional academic/non-profit citations such as the one above. Your understanding and cooperation is most appreciated!

Jim

1:23 am on Dec 31, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 12, 2003
posts:51
votes: 0


Excellent, JD! It seems to be working great. However, things seem a bit slow! Actually much slower than I expected. Does all this rewriting cause THAT big a lag in performance? Or am I better off setting things up antoher way in httpd.conf?

Once upon a time, I had seperate virtual host entries for each domain, so that when people typed in mail.theirdomain.com it redirected them by the document root. Is that a better way of doing it, performance-wise?

The thing is... I may be getting a lot more virtual hosts in the next couple of months, so I don't want to have that many vhosts in the config file bogging things down.

And yes, if somebody can recommend something for my inability to put these declarations outside a virtual host, I'd be grateful. Maybe I need to change something else that I've got set?

1:44 am on Dec 31, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Donboy,

I suspect the slowdown is due to the proxy function. If all this stuff is actually on the same server, you don't need a proxy and you can just do a server-internal redirect. Or use symlinks to map the mail subdirectory into each user's space - there are many ways to do it.

I've got htaccess files with hundreds of lines of code in them (trying to clean up someone else's old messes), and despite the facts that the majority of the code is processed for each and every file request, and that htaccess code is interpreted for each request rather than being pre-compiled like httpd.conf, the slowdown is imperceptible compared to network and disk latency. So, my gut feel is that it's the proxy component that is causing the slowdown. Plus, any external 301 redirects will invoke a "handshake" with the browser for each file requested, so you want to avoid those, too.

I'm a "rented-server-space" user - actually, my clients are the users - so I hope some server-admin gurus will step in here and help you to define your root objectives and then help you find the most efficient way to implement them.

Jim

7:04 am on Dec 31, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 12, 2003
posts:51
votes: 0


Hmmm... what you're saying makes a lot of sense. In my case, I have direct access to the config file and I'm adding all of my rules there.

One thing: I have everything on the same computer, so I guess redirect would be fine... but can I redirect by domain? The only examples I've seen with redirect were for individual files or directories. I've never seen that done with a domain. Also, will that keep the URL the same? One thing I want to do is keep the URL the same in the browser so that the user isn't confused when they realize they have been redirected to another site. It's all hosted on the same box, but that's beside the point. Does redirect enable you to keep the URL the same in the address bar?

Also, how do you do what you described with symlinks? If the user enters mail.somedomain.com in the browser, how does the symlink know when to do its part?

4:39 pm on Dec 31, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Since you've got everything on the same box, you can work in file system space, not URL space. Instead of doing a URL-redirect, do a filepath-substitution.

This is like the "Shortcuts" on your Windows desktop - the shortcuts act as proxies for files and folders located elsewhere in the filesystem. If you create a symlink called webmail in each user's filespace, and then point that symlink to the real webmail-handling directory, then it will appear to the user that they have a webmail subdirectory that they can access directly, except that they don't really: all they have is a unix link to that directory, and you as admin control all the permissions in that directory. So, no change of URL is needed.

This is a subtlety that isn't always immediately obvious... mod_rewrite works on the edge, at the very end of the process of handling URLs, and at the very beginning of handling filenames. It lives in both worlds, and can be used in both. So, Apache gets as far as it can using URLs, and then once the URL is fully resolved, then it starts looking for unix filesystem files. A second subtlety about servers is that a URL-name does not have to have anything to do at all with the filename. If you think about all the CGI scripts out there that "look like" a web page to the user, then it becomes clearer. The URL http://www.example.com/widgets.html may or may not point to a file named widgets.html; We often do that to keep things simple, but it is not at all required.

So, the trick here with your application is to figure out how far you use the URL and then what you do on the filesystem side. Mod_rewrite can be used to map URLs into the filesystem, do HTTP (external) redirects, and internal filepath changes within a user's filespace. Then symlinks can be used to re-map (sort of file-redirect) inside the unix filesystem, and can cross between users and admin directories.

I sure wish I could just draw this out on a piece of paper - it would make it a lot easier to explain. Try putting a symlink into one of your test user-directories, and point it to some file in another directory and see how it works. For example, you should be able to put a symlink into user joe-bob's account space that makes it look like joe-bob has a copy of httpd.conf in his home page directory. Set the permissions so he can't edit it! It is only a link, but it makes it look like he has a local copy of the file.

I may be over-extending my unix experience somewhat here, but I can tell you that in 25-plus years of computer hardware and software design, I have never typed anything that made a computer literally catch on fire, so I encourage you to experiment with this stuff! :)

Jim

6:15 pm on Dec 31, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 12, 2003
posts:51
votes: 0


No, I think I'm following you ok. My question is... if I want a user to go to mail.theirdomain.com how can I have a symlink that takes them to another area of my filesystem? I understand that I could put a symlink called "webmail" that takes them to another webmail that's installed on another area of the server, then a user could point their browser to www.domain1.com/webmail and they would be taken to the right place. But I don't want them to see the /webmail part. I just want them to see the top-level domain of mail.domain1.com.

If you have any other suggestions that may be "outside the box" from our discussion about rewrites and symlinks, I'm open to anything. For example, if there's something I can do in DNS that would redirect the user properly, that'll work too... but I would like to have the URL private so they don't realize they have been redirected to another TLD.

2:12 am on Jan 1, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 16, 2000
posts:122
votes: 0


I'm open to anything. For example, if there's something I can do in DNS that would redirect the user properly, that'll work too... but I would like to have the URL private so they don't realize they have been redirected to another TLD.

DNS is the best and easiest way along with Apache VirtualHosts.
You need a dedicated IP that is not used by any domains.

In each domain's zone file point mail to the dedicated IP and use an A record. Don't use a CNAME

example.tld. IN NS ns1.dns.tld.
example.tld. IN NS ns2.dns.tld.
example.tld. IN MX mail.example.tld.
www IN A 01.01.01.01
mail IN A 02.02.02.02

Apache loads VirtualHosts in the order they appear so you can use this to your advantage by creating an IP VirtualHost and making it the first one in httpd.conf

<VirtualHost 02.02.02.02>
ServerName 02.02.02.02
DocumentRoot /usr/local/webmail
</VirtualHost>

3:44 am on Jan 1, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 12, 2003
posts:51
votes: 0


Hey! That's a great idea. But one problem to add to the mix... I'm behind a router, so all I can do is port forwarding. I have ifconfig'd my system to answer to both 192.168.0.50 and 192.168.0.51, but I'm not even sure if that's useful. Yes, I can forward anything for a particular IP to my server, but it has to be using a port that's not currently in use, or my router will throw a fit about forwarding the same port to 2 different IPs. Like normally i would want to forward these requests to port 80. Even if I tried forwarding to a non-standard port like 81, the user would need to enter this port number into their browser's address bar, right? Kinda messy.

I am currently in the process of setting up djbdns on my machine and I'm almost there. Meanwhile, I am modifying my A records using Network Solution's advanced utility for modifying your domain.

After I get DNS working, I am going to try putting the server on the DMZ so that it can answer requests for the IP addresses I have directly. I have a static line with 2 IPs currently allocated to me, but only one of them is in use at the moment, which is feeding my router. I also have to learn iptables so I can setup a basic firewall, but that's still another set of problems I have to overcome first. Until then, I am using the router's firewall to protect my system.

Maybe I need to get that all setup first, then I can start using that second IP for all traffic concerning mail.*.com.