redirect from subdomain to different host without 301 - apache

I need to point from someSub.somedomain.com to mysub.mydomain.com, and I need the url of the site to continue reading someSub.somedomain.com
I've tried many variations of:
RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]
But I can't seem to get anything to work. Any advice?

This answer kind of already answers this question:
https://serverfault.com/questions/506623/masking-the-url-in-a-mod-rewrite
But it appears to me what you are looking for is a reverse proxy.
In the virtual host for your server:
ServerName somesub.somedomain.com
ProxyPass "/" http://mysub.mydomain.com/
ProxyPassReverse "/" http://mysub.somedomain.com/
Now when you go to your http://somesub.somedomain.com all the requests will behind the scenes actually be going to http://mysub.mydomain.com but the browser user won't see that.
There are various customizations to this you can make.
Don't forget to enable mod_proxy

Related

Apache redirect after a rewrite

I'm trying to figure out how to properly do this. I'm hosting a domain that used to have a website also on the same server, however the website has now been moved to a different machine, but they want to keep the domain hosted on our DNS. Rather than changing the DNS record right now, I'm trying to figure out how to do a proxy redirect but I'm having some trouble.
Right now, I'm using the RewriteEngine to rewrite the URL as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.org [NC]
RewriteRule ^/(.*) http://www.domain.org/$1 [L,R]
This is in case someone looks up the website simply by http://domain.org it will get rewritten to http://www.domain.com - that works fine.
Now I need to redirect it to go to an IP address with a username:
http://111.222.333.444/~user
Rather than simply redirecting it to that address, I'd like to do a proxy where the domain will still be visible in the browser's address bar, while also keeping the above rule in place.
Suggestions anyone?
Make sure mod_proxy is enabled and do:
<VirtualHost *:80>
ServerName www.domain.com
ProxyPass / http://111.222.333.444/~user
</VirtualHost>

Apache mod rewrite to direct from one server to another

I've got to apache web servers running. One on port 80 and another on port 8077.
I'm wanting to try and redirect all the traffic through the port 80 version if possible.
What I'm ideally wanting is to be able to go to http://translate.example.com
and the traffic get directed to http://www.example.com:8077
I've already got a lot of mod_rewrite going on at the main port 80 server, but I'm not sure which of the servers needs configuration or whether both do.
I'm wanting to make sure that translate.example.com/img (or any other subdirectory) actually points to the 8087/images directory.
update
I've now got the following:
RewriteCond %{HTTP_HOST} example[NC]
RewriteCond %{REQUEST_URI} ^/glot$ [NC]
RewriteRule ^(.*)$ http://www.example.com:8077/$1 [P]
ProxyPassReverse / http://www.example.com/
I'm getting to see the other servers new pages, but I'm finding all the resources aren't found like images, css etc
Doing a view source all the resources in the installed product are set with leading slash
For example
/img/glotpress-logo.png
So I'm not sure how to get the resources loaded up.
Note I'm happy enough if the original starting point is www.example.com/glot instead of glot.example.com as in the original question
You can remap your resources to another server but having the other server on non-default port might prevent some of your visitors from viewing them as they might be block (firewalled) from accessing the port. If you're not worry about the port being block you can use mod_rewrite Remapping Resources. method.
RewriteEngine On
RewriteRule ^/images/(.+) http://www.example.com:8077/images/$1 [R,L]
If you want to make sure that everyone is able to view the external resources, you need to use proxy where apache will tunnel the visitor connection to example.com:8077 transparently. You can read more about mod_rewrite for Proxying at apache website.
RewriteEngine on
RewriteBase /images/
RewriteRule ^images/(.*)$ http://example.com:8077/images/$1 [P]
ProxyPassReverse /images/ http://example.com/images/
UPDATE
Have you tried to remove
RewriteCond %{HTTP_HOST} example[NC]
This line basically tells that it will only process if the HTTP_POST is example.com if its coming from www.example.com this rule is not applicable. I'm hoping that "example[NC]" is a typo.
In the end it probably looks like
RewriteRule ^/glot/(.*)$ http://www.example.com:8077/glot/$1 [P]
ProxyPassReverse /glot/ http://www.example.com:8077/glot/
You need to do the configuration at the port 80 server to make it act as a proxy for the 8077 server.
The Apache document is here: http://httpd.apache.org/docs/trunk/rewrite/proxy.html

Changing the URL within .htaccess

I'll try and explain this the best I can; I have two domain names: www.original.com and www.mysite.com/. Within the site for www.original.com there is no content and all the content is hosted in www.mysite.com/original/. Now, when I visit www.original.com I have set up a redirect like this:
redirectMatch 307 ^(.*)$ http://www.mysite.com/original
However, I have realised that this is not what I need, because I would like the visitors of the www.original.com site to see www.original.com on the address bar, and not http://www.mysite.com/original. Is there any way to do this in .htaccess? I used to do it in the cPanel of my old hosting account via an addon domain, but the hosting I have now (united-domains.de) do not provide addon domains, apparently.
Thanks
Since the 2 sites are separate, you'll need to rely on mod_proxy. There are a number of ways to create a reverse proxy from the www.original.com domain to the www.mysite.com domain. The easiest would be to use ProxyPass and ProxyPassReverse:
ProxyPass / http://www.mysite.com/original
ProxyPassReverse / http://www.mysite.com/original
But these are only applicable inside the server or vhost config. If you don't have access to that, you'll have to use mod_rewrite's [P] flag, which hands the target substitution of a RewriteRule to mod_proxy, and you can use this within an htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?original\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/original/$1 [L,P]
You have to tell www.original.com to act like a proxy server. but this may require you more than just editing an htaccess.
See this for more informations : http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

Proper ProxyPassReverse with rewrite

I'm trying to do something similar to the Coral cache.
For example if a client/browser looks up google.com.foo.com, it gets passed to a proxy by apache and serves a proxied/cached version of google.com to the client/browser.
My configuration for this so far is this:
ProxyRemote * http://localhost:8080
RewriteEngine on
RewriteCond %{http_host} ^(.*)\.foo\.com [NC]
RewriteRule ^(.*)$ http://%1$1 [P]
This actually works well as long as the website does not redirect to another site, but as soon as it redirects, it "bursts" out of the proxy and simply goes to the redirected site (oviously).
ProxyPassReverse should, as far as I understand it, prevent this, but I simply can't piece together how my ProxyPassReverse directive should look. After all, the hostname could be everything in this case...
Is this even possible?

RewriteRule for mapping x.domain.com to y.domain.com

Is it possible to redirect all requests to x.domain.com/.* to y.domain.com/.* WITHOUT letting this redirection be visible in the url?
I have unsuccessfully tried several things in .htaccess. Just specifying the [L] flag still shows this redirection in the url (as it does when I use the [R] flag additionally).
EDIT: as somebody claimed there being no reason for this, let me give some more information :)
I have one nice url: x.domain.com , which is well known.
Then there are a number of other domains: spring.domain.com , summer.domain.com , autumn.domain.com, winter.domain.com .
Depending on the time of the year, a specific y.domain.com becomes the current one. The x.domain.com should always map to the current one.
EDIT2:
I'll write here, as the code isn't nicely rendered in the comments...
I tried what Arjan suggested:
RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1
Unfortunatly though this keeps redirecting forever. :(
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Putting the [R] flag behind, I see in the url something like:
http://x.domain.com/path/to/y.domain.folder/path/to/y.domain.folder/path/to/y.domain.folder/ ...
Any suggestions?
Now that I can read the errorlogs, I can give a direct response, as what a possible 500 error refers to.
Assuming you have access to the Apache configuration, create the following virtual host for domain x.domain.com. Then simply update y to whatever you need each season.
<VirtualHost ...:80>
ServerName x.domain.com
UseCanonicalName Off
ProxyRequests Off
<Proxy *>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPreserveHost Off
RewriteEngine On
RewriteRule ^$ http://y.domain.com/ [P,NC]
RewriteRule ^/(.*)$ http://y.domain.com/$1 [P,NC]
ProxyPassReverse / http://y.domain.com/
</VirtualHost>
Also to pick up the Alias suggestions, if you have multiple virtual hosts (one for each season) then you could put a server alias into the current domain. E.g.
<VirtualHost ...:80>
ServerName summer.domain.com
ServerAlias x.domain.com
...
</VirtualHost>
<VirtualHost ...:80>
ServerName spring.domain.com
...
</VirtualHost>
...
This would make Apache deliver the summer.domain.com pages if you go to x.domain.com. If your seasonal subdomains depend on the HOST header line to be set correctly (i.e. to season.domain.com) you would need to use the first suggestion above, though.
If these are not hosted on the same server, then you'd need the Proxy flag. This also requires the proxy module to be running. Not tested:
RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ http://y.domain.com/$1 [P]
EDIT: Given the edits to your question they're probably just on the same server. So then indeed, as jetru suggested an Alias might do. Or:
# No RewriteCond required; serve all content from other folder:
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1
EDIT: The above would not change the HTTP_HOST header that was sent by the browser (maybe that can be done as well). This implies that it would only work if the subdomains are represented on the file system as separate directories. So, as the .htaccess would then be placed in the directory holding the website for x.domain.com, the RewriteCond wouldn't even be required. Also, the directory for this x.domain.com subdomain would in fact not need any HTML content then; in the end all content would be served from the directory of another subdomain.
EDIT: As the above does not seem to work either, and yields endless rewrite loops even when adding [NS], maybe simply adding [L] helps here:
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [NS,L]
Or maybe one can set an environment variable to stop the loop:
RewriteCond %{ENV:MY_VAR} !=1
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [E=MY_VAR:1]
But, for both [L] and [E]: I'm just guessing; I've never made mod_rewrite jump into the directory of another virtual host. I am not sure it can be done to start with.
Unfortunately, it's unclear how one would add a new subdomain. If one would just need to create a new directory with the name of the subdomain (without any use of some administrative tool) then the provider might be be using system wide rewriting as well. In fact, even without subdomains the provider might be doing some Mass Virtual Hosting as described in the URL Rewrite Guide.
I guess the best solution would be to change the value of HTTP_HOST on the fly, to solve issues with any system wide rewriting. Maybe the following is allowed to achieve that:
RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [E=HTTP_HOST:y.domain.com]
Again, as the above would only be present in the .htaccess in the x.domain.folder, the RewriteCond is probably not needed at all.
Have you tried
Alias /dir/file.html /full/path/to/other/file.html
??
To my knowledge and testing with firebug a redirect via .htaccess is always announced to the client and it's up to him how to proceed. It is therefore not an alternative to some sort of SSI functionality. To prevent a "fake" address modern browser should always make the REAL address visible to the user, however I think I have seen some misbehavior in programs like "feeddemon" where IE is embedded. If you - for whatever reason - really want to show content from one subdomain on another you can try using Javascript or (i)frames on the user side or some include functionality on the server site (eg. file_get_contents with php). However, I don't recommend this.