Knowledgebase

mod_rewrite to another domain on another server

Posted by MrSparky, 08-28-2007, 07:16 AM
Hey, So - I'm trying to do the following: Have multiples domains on multiple servers, with a single server handling them all. I need mod_rewrite to map: http://www.olddomain.com/abc.php -> http://www.newdomain.com/SITE1/abc.php But do it silently - so a [301] Redirect or similar won't do - www.olddomain.com is still the domain I want everyone to use *always*. I've seen ProxyPass (and mod_proxy) but it appears that these need to be done at an httpd.conf-type level - how can this be achieved with mod_rewrite? Effectively, I just want *everything* on a certain domain redirected to another domain silently in the background. In actual fact, it'll 99% of the time be on the same server - but I think the principle should be the same anyway. I've tried: RewriteEngine on RewriteRule / http://sitemanager.com%{REQUEST_URI} [P] Any help would be appreciated! Thank you!

Posted by foobic, 08-28-2007, 07:38 AM
mod_rewrite will only do this with the [P] flag (force proxy), which requires mod_proxy enabled. If the reason you don't want to use httpd.conf is you're trying to do this on a shared hosting account, forget it.

Posted by MrSparky, 08-28-2007, 07:45 AM
mod_proxy is actually enabled - and it's a dedicated server I run. However, the control panel is a very messy one (and we won't be using this server for long - but I still need the same anyway) which has the .conf files all over the place. I attempted to edit one of them but it obviously was overridden somewhere down the line - as it didn't make a difference. So, if there's a .htaccess way that would be great. If not, I'm not sure exactly how to find the authoritative .conf file to edit. Cheers

Posted by foobic, 08-28-2007, 08:28 AM
Ok, well the mod_rewrite way (straight from the manual) would be in httpd.conf: or in .htaccess: You'd do your system a favour by finding the correct httpd.conf and putting it there so Apache doesn't have to read .htaccess for every request.

Posted by MrSparky, 08-28-2007, 10:17 AM
foobic, Thanks for the help - however, the following error appears (in /var/log/error_log) Invalid command '^/(.*)', perhaps mis-spelled or defined by a module not included in the server configuration And an internal server error is displayed when trying to view the site. Any thoughts as to why?

Posted by foobic, 08-28-2007, 10:42 AM
Um, perhaps because I left out the "RewriteRule"

Posted by MrSparky, 08-28-2007, 10:53 AM
I thought you had, so did try that! But, it still comes back as a 404 ... Shall I just throw the thing out the window now, or is there a logical reason?

Posted by foobic, 08-28-2007, 04:11 PM
There's always a logical reason. Unfortunately it's usually a logical reason you didn't anticipate Turn on rewrite logging and see what Apache thinks it's doing.

Posted by Steve_Arm, 08-28-2007, 05:23 PM
Options +FollowSymLinks and if you are one level down the root directory of the virtual host: RewriteBase /dir_name Where ^/somepath(.*) would be ^somepath/(.*)$

Posted by MrSparky, 08-29-2007, 03:50 AM
Steve_Arm - thank you, that explains away some of the problems. I had already done the FollowSymLinks (from previous Mod_Rewrite experiences) but the RewriteBase is, now obviously, required as it is in a subdirectory. It still doesn't work, but at least we're closing in! :-) Foobic - I've turned on RewriteLog to /var/log/rewrite - the file exists, but nothing is written there. Is there any certain reason? (I'm going to research about the permissions that RewriteLog may need, as perhaps that's not within Apaches 'reign of control' - but then, it wouldn't have been able to create it in the first place ...)

Posted by Steve_Arm, 08-29-2007, 03:58 AM
Do you want to post what you have so far?

Posted by MrSparky, 08-29-2007, 04:04 AM
Certainly: Options -Indexes +FollowSymLinks RewriteEngine on RewriteBase /testsite RewriteRule ^/(.*) http://www.sitemanager.com/$1 [P] RewriteLog is not logging anything. I've changed the position to one I found in one of the .conf files itself - /var/log/apache2/rewrite.log - this ensures permissions etc. But, to no avail - I've also increased RewriteLogLevel - not logging a thing.

Posted by Steve_Arm, 08-29-2007, 04:18 AM
It works for me like this: Options -Indexes +FollowSymLinks RewriteEngine on RewriteBase /testsite RewriteRule ^(.*)$ http://www.sitemanager.com/$1 But I haven't installed mod_proxy so I wouldn't know if the P flag is workin

Posted by foobic, 08-29-2007, 04:19 AM
Are you putting these in a VirtualHost or in the main server config?

Posted by MrSparky, 08-29-2007, 04:20 AM
Here's my RewriteLog - I managed to get it going with a bit of tinkering! (This is for a single page transaction - obviously, towards the end, the rewriting is producing an error page)... 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81dfdb0/initial] (2) init rewrite engine with requested uri /testsite/abc.php 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81dfdb0/initial] (3) applying pattern '^' to uri '/testsite/abc.php' 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81e3dc0/subreq] (2) init rewrite engine with requested uri /testsite/abc.php 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81e3dc0/subreq] (3) applying pattern '^' to uri '/testsite/abc.php' 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81e3dc0/subreq] (4) RewriteCond: input='' pattern='(.*)/[^/]+' => not-matched 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81e3dc0/subreq] (1) pass through /testsite/abc.php 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81dfdb0/initial] (4) RewriteCond: input='/domains/m/y/myserver.co.uk/public_html/testsite/abc.php' pattern='(.*)/[^/]+' => matched 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81dfdb0/initial] (1) pass through /testsite/abc.php 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81dfdb0/initial] (3) [per-dir /domains/m/y/myserver.co.uk/public_html/testsite/] strip per-dir prefix: /domains/m/y/myserver.co.uk/public_html/testsite/abc.php -> abc.php 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81dfdb0/initial] (3) [per-dir /domains/m/y/myserver.co.uk/public_html/testsite/] applying pattern '^/(.*)' to uri 'abc.php' 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81dfdb0/initial] (1) [per-dir /domains/m/y/myserver.co.uk/public_html/testsite/] pass through /domains/m/y/myserver.co.uk/public_html/testsite/abc.php 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81ea9d8/initial/redir#1] (2) init rewrite engine with requested uri /.error 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81ea9d8/initial/redir#1] (3) applying pattern '^' to uri '/.error' 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81e3dc0/subreq] (2) init rewrite engine with requested uri /.error 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81e3dc0/subreq] (3) applying pattern '^' to uri '/.error' 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81e3dc0/subreq] (4) RewriteCond: input='' pattern='(.*)/[^/]+' => not-matched 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81e3dc0/subreq] (1) pass through /.error 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81ea9d8/initial/redir#1] (4) RewriteCond: input='/domains/m/y/myserver.co.uk/public_html/.error' pattern='(.*)/[^/]+' => matched 123.123.123.123 - - [29/Aug/2007:09:17:49 +0100] [myserver.co.uk/sid#818e028][rid#81ea9d8/initial/redir#1] (1) pass through /.error

Posted by MrSparky, 08-29-2007, 04:23 AM
foobic - these are going in the .htaccess for that directory. Steve - that's superb, it actually does the redirect - HOWEVER - without the [P] flag, it actually redirects the entire site *client side* so the user sees the second site. It needs to be using a proxy so they never see the second site (thus, many sites can work off the same master server without the user needing to know the specifics). This happens *with* the [P]... Bad Gateway! The proxy server received an invalid response from an upstream server. The proxy server could not handle the request /GET/testsite/ Reason: DNS lookup failure for: http://www.SITEMANAGER.com

Posted by Steve_Arm, 08-29-2007, 04:29 AM
Right now I'm testing it from a virtual host, but the directory is an Alias, so it's: # various configurations Alias /directory /usr/home/username/public_html/directory.com RewriteEngine On RewriteBase /directory.com RewriteRule ^somepath/(.*)$ http://www.domain.com/$1 [L] So when I call the URL it is: http://1.1.1.1/directory.com/somepath

Posted by Steve_Arm, 08-29-2007, 04:33 AM
Maybe you want to use the directive ProxyPass

Posted by foobic, 08-29-2007, 04:35 AM
Steve, my question was to MrSparky, but since he's got the logging working now it's irrelevant MrSparky - have you got the correct resolver settings in /etc/resolv.conf? Can you use lynx / wget from the command line?

Posted by Steve_Arm, 08-29-2007, 04:37 AM
Ha! I didn't looked at the username! I thought MrSparky has written it.

Posted by MrSparky, 08-29-2007, 04:51 AM
I can wget although it comes back (correctly) as a 404... that shouldn't mess it up, should it? I've tried ProxyPass but it wasn't working - possibly because the sites are assigned like: Directory /*/*/*/public_html/.... (As in /m/y/mysite.com/public_html) But using RegExp to find the correct directory based off the domain name. So each site doesn't have it's own Directory or VirtualSite in the httpd.conf files

Posted by MrSparky, 08-29-2007, 05:04 AM
Got the b**tard! For some reason - the bad proxy request was that it couldn't find sitemanager.MYDOMAIN.com - even though 'wget' worked fine for it. At least I can narrow down the problems now. Guys - Thank you SO much! Mod_Rewrite is cryptic at best! I've managed to get it to do some useful (wonderful!) things that are very basic, but sometimes it stumps me! Appreciate the help very much!! EDIT - You'd believe me if I said it finally decided the original host was, after all, fine! And it's not DNS issues - the subdomain has been up for quite a while, not recently added! Cheers again! Last edited by MrSparky; 08-29-2007 at 05:14 AM.

Posted by MrSparky, 08-29-2007, 05:38 AM
Interesting thought - how would I only redirect traffic where there is no existing local file? The only way I can think if using a 404 page that is in another directory and transferring all that directory to the proxy - but this would require two hits to the .htaccess file and isn't 'elegant'. What would be nice would be: RewriteRule PageIsA404 RewriteRule RegExp http://blahblah.com [P] Any thoughts? If not, I'll just try my method, or dig deeeeep into ModRewrite manuals :-)

Posted by foobic, 08-29-2007, 05:42 AM
RewriteCond %{REQUEST_FILENAME} !-f Glad you got it sorted

Posted by MrSparky, 08-29-2007, 05:46 AM
Is it possible that Mod_Rewrite is the most powerful aspect of web programming? Honestly, as an experienced PHP programmer there is absolutely NOTHING that comes close to being able to manipulate what the user sees quite so effectively! THANK YOU!! Great help!

Posted by Steve_Arm, 08-29-2007, 05:49 AM
mod_rewrite and sendmail configuration

Posted by MrSparky, 08-29-2007, 05:54 AM
No. You can take sendmail off that list, thank you very much. ;-) (Then again - based on the same logic - mod_rewrite is also off the list. So, they're both powerful as hell, seriously powerful! And seriously complicated!!) Last edited by MrSparky; 08-29-2007 at 06:02 AM.

Posted by david510, 08-31-2007, 09:00 AM
Ahh I see it is sorted. Did not see that Edited. Last edited by david510; 08-31-2007 at 09:03 AM. Reason: just noticed the this was sorted.



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
CSF v APF and BFD (Views: 606)
Theprimehost Review (Views: 572)


Language:

Client Login

Email

Password

Remember Me

Search