1 domain.. 2 server and 2 applications - linux

i have a site like twitter.com on server one and on server two i have forum, which path is like domain.com/forum
on server one i wanted to implement wild card dns and put main domain on it. but on server two i wanted to keep forum separate, i cant give sub-domain forum.domain.com, because all its links are already put in search engines and link back to domain.com/forum.
so i was wondering, how can i put domain and wild card dns on server one and still able to give path on server 2 for domain.com/forum (as sub-folder).
any ideas?
do you think htaccess can do that job? if yes, then how?

You could use htaccess and mod_rewrite so domain.com/forum actually displays pages from forum.domain.com.
Maybe something like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule domain.com/forum/(.+) forum.domain.com/$1

Easy - use a proxy! If you like apache you will love apache mod_proxy for your purpose.
<VirtualHost *:80>
ServerName maindomain.com
ServerAlias *.maindomain.com
# insert document root and general settings for this domain here
# ...
ProxyPass /forum http://forumdomain.com
ProxyPassReverse /forum http://forumdomain.com
ProxyPassReverseCookieDomain forumdomain.com maindomain.com
</VirtualHost>
This configuration makes apache do a HTTP-request to your internal domain (forumdomain.com) without notifiying the users browser of the internal location. Your Forum will be accessable at http://*.yourdomain.com/forum. Cookies and headers the forum sents will get rewritten accordingly and Search-engines will not take notice of your backend-server.
You can read more about it at http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
Should you need to rewrite reference sin your html (href, src ...) you might google on "mod_proxy_html".
A solution like this could of course be build with other intelligent proxyservers like squid as well. You can use it to map any content from "backend servers" to your public domain.
Make sure routing is OK or set up a host-entry for your internal domain (forumdomain) with a internet ip-addresse 192.168 ...
Enjoy your site and give feedback how worked out :)
p.s.: a "RewriteRule" directive can potentially do the same thing for you but the rdirect will be visible (and executed) by the client unless you specify the "P", foricng it to do an internal proxy request. If available I would prefer the mod_proxy though as it is more versatile and allows for more configuration.

If you use a proxy on server 1 pointing to server2, you will increase the load on server 1, since all traffic will be routed through it. Besides, if server 1 goes down, nobody will be able to reach server 2 either. Of course it is possible though, but these things are to be considered.
I would suggest setting up a supdomain for server 2 anyway, like forum.domain.com, and on server 1 you set up a 301 redirect from domain.com/forum to forum.domain.com using mod_rewrite from htaccess. Using that technique, you can even redirect calls to specific links to their corresponding page on server 2. Search engines will follow the 301 and they will eventually update the index.

You can use a 301 redirect to make sure the search engine update their index with your new urls like so:
RewriteRule domain.com/forum/(.*) http://forum.domain.com/$1 [R=301,L]

If you've got two servers you don't really have much choice but to use a redirect (ideally a 301 Permanent redirect) to move users from domain.com/forum to forum.domain.com.
The only other way to do it would be to put a reverse proxy in front of those two servers, which reads the URL and internally directs the query to the right server, but that's then an extra piece of hardware.

Related

How do I redirect to a different domain AND a different folder?

What htaccess rule would I need to apply to redirect to a different domain but also to a different folder structure? Example: current page is
http://oldwebsite.com/school/forum
and the new website would be
https://newwebsite.com/forum
I figured that the rewrite rule for the domain name alone would be
RewriteEngine on
RewriteRule ^(.*)$ https://newwebsite.com/$1 [R=301,L]
which works just fine when I test it.
But I can't find out what rule I'd need for rewriting the old forum posts that are in an unnecessary subfolder to the new domain where it's directly in /forum.
Also: would I need to write https or is that something that doesn't matter and will be done automatically if I have an SSL certificate and the forum (phpbb3 in this case) is set to SSL anyway?
Thanks and best regards,
Andi
This is what you are looking for I assume:
RewriteEngine on
RewriteRule ^/?school/(.*)$ https://newwebsite.com/$1 [R=301]
That rule will work in the http servers host configuration and likewise in a dynamic configuration file (.htaccess) then the hosts document root. Obviously the rewriting module must be enabled. If you decide to use a dynamic configuration file, then you additionally need to allow that using the AllowOverride directive.
You need to specify the https protocol, since that is where the redirected client will make the connection to. That is something the client has to decide before having contact with the server. So whether a ssl certificate is installed does not really help in that. You mighty rely on an additional redirection from http to http on the new domain, but why? Much better to redirect directly to the ssl port in the first place.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Redirect to URL and keep it hidden

I have this small private minecraft server, and I added dynmap.
So I would like to share this map on website, BUT I don't want to use its original URL, cause it shows the server IP that I would like to keep hidden.
Map on URL:
http://server17.mycommunity.net:8123/
I want it to be seen from:
http://minecraft.mycommunity.net/map/
I would like to do this with .httaccess file. I've tried the settings below, but it only redirects to the server17 address, and I would like it to stay in /map/ URL and keep the server address hidden.
RewriteEngine On
RewriteBase /
RewriteRule ^map/$ http://server17.mycommunity.net:8123/ [L]
Is this possible to do it with .htaccess or do I have to do this some other way?
These answers looks to be close to what you need, although you might need mod_proxy installed:
configuring ProxyPass on .htaccess to show tomcat through apache http server
.htacess rewriting with the [P] proxy flag
I don't use Apache regularly, so I'm not quite sure what is needed. I do know that with some configuration you can use mod_rewrite/mod_proxy to proxy content from one server at one port to another at the regular http port. That's what you are really needing. There is no such thing as rewriting when it comes to proxying.
However, I'm not sure exactly what that proxy entails, or if, by setting up this kind of proxy, you aren't breaking anything.

How do I redirect a subdomain to a different domain?

How do I redirect this:
www.sub.example.com
to this:
www.example2.com
How is this done -- 301? .htaccess? Can someone point me in the right direction please? My host is NearlyFreeSpeech and the domain I want to redirect to is quite a large site with a static IP.
There are multiple ways to do this:
If you have control of the domain, you can use a CNAME record provided by your DNS server.
In Apache, you can specify a Redirect or RedirectPermanent directive in your Apache configuration files (httpd.conf, .htaccess, etc.)
In Apache, you can specify a reverse proxy via the ProxyPass directive. Here's some documentation detailing this solution.
Other solutions are possible, and generally vary by server and configuration stack. I assume Apache compliance, given the presence of .htaccess in the problem statement.
Here's a Wikipedia reference on redirects via the 3xx family of HTTP status codes, if anyone's curious about the client's perspective.
Something like this should do it
RedirectPermanent / http://www.example2.com
RedirectPermanent would return a 301 code and force the client to go to www.example2.com, it works in apache config file and .htaccess file. You need mod_alias enabled to use it.
You can use this on the root directory of the subdomain in a .htaccess file, or in the virtualhost section of your apache configuration file.
For your reference the documentation

Is there a way to always require or force the 'www' subdomain on a site?

I want to prevent users from going to say example.com and only go to www.example.com, we are using IIS 6. So say they go to example.com it could tack on the www.example.com, etc.
Is this a setting somewhere or will I have to code it to check for the subdomain when they land and redirect accordingly?
EDIT: I know the best way is to move away from the www prefix but for whatever reason if the user launches a course (this is an LMS) without the www in the URL the tracking does not work for the .asmx file, that is why I am trying to force the 'www' because if some people don't have it then they wonder why the tracking does not work.
If you're using ASP.NET create an HttpModule, handle the BeginRequest event and add this code inside your handler:
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (context.Request.Url.Host == "example.com")
{
context.Response.Clear();
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", "http://www.example.com" + context.Request.RawUrl);
}
Note that I didn't use Response.Redirect(), this is done for the sake of SEO, as Response.Redirect() always returns status 302 which means the object was moved temporarily while status 301 means the object was moved permanently, this will keep the PageRank of your pages from being divided between the www and the non-www versions (if search engine crawlers can access the page using both the www and the non-www URLs, they will divide your PageRank between the two, hence is the use of 301 which search engine crawlers understand and will keep your PageRank to only the www version of your site).
As both records already point to the correct server...
...you could simply set up a new website in IIS (server version needed) and have it respond only to example.com (the host header setting) and have it redirect to the wanted url (check redirect to url in Home Directory tab and enter www.example.com). The original site should then handle it (you could set it's host header to answer to www.example.com to be more specific).
If you can't do that on the web server, your publishing firewall should be able to, or you might consider replacing it. Your DNS provider might also provide (pun not intended) a redirect service (doing basically the same thing as above for you I guess).
As per other responses, arrange for a 301 redirect from the unadorned domain name to the site with the www. prefix.
Given that I actually work in the DNS industry, I'd like to share my views on the www. debate:
For now, at least, IMHO, the preferred version of URLs should be with the www. prefix. The hostname part of a URL is exactly that, it is a hostname. The only DNS resource records that your browser will search for are A (and possibly AAAA for IPv6) records, and the resulting IP address is that which it'll connect to.
It is not a web site address - only the full URL (with the http:// prefix) specifies that this host is expecting to receive HTTP connections on port 80.
The whole reason for the www. prefix in the first place was to allow for the separation of different protocols to different hosts. As Verisign showed when they (briefly) introduced their "SiteFinder" service several years ago, assuming that every request for an A record is for the use of the HTTP protocol is a massive mistake.
Having the canonical version of your URL be the one with the www. prefix also makes cookie handling easier, and allows for easier splitting of static content to content delivery networks (as recommended by Yahoo!, Google, etc).
Now, there is a DNS record type (SRV, see RFC 2782) which uses a service and transport prefix to allow a single domain name to dispatch different protocols to different hosts (and hence IP addresses).
The ideal DNS set up would be a record that looks like:
_http._tcp.example.com IN SRV 10 0 80 www.example.com.
This says that all requests for HTTP URIs over TCP/IP should be addressed to TCP port 80 on the hostname www.example.com. Note that with this syntax you could also have HTTP services automatically server from ports other than port 80 without the port number being part of the URL.
The SRV record is a required part of SIP, and is commonly used for Jabber (XMPP). However AFAIK no browser uses it. :(
Contact your host (or your domain registrar) and have them set it up to work with or without www. It should however work both ways or only one way if you so wanted. I'm assuming you have a host and are not running your own web server/domain name registration, lol.
This is how it works, regardless of whether or not you should.
This is handled by DNS. Usually, example.com and www.example.com will point to the same server, but they don't have to. If www.example.com isn't explicitly registered, then the request gets sent to the example.com server, and that server gets to decide what www.example.com means, as well as mail.example.com, ftp.example.com, home.example.com, users.example.com, and so on.
Edit: I realize this story is sort of incomplete, as these days Apache gets handed the URL that you used to get in with, and it may interpret a subdomain from that, and direct the request to a virtual site. But this shouldn't be a factor at the "www" level.
The typical way this is done by web sites is to do an HTTP redirect from example.com to www.example.com. You do this by returning either HTTP Status code 301 (permanent redirect) or 302 (temporary redirect) when user goes to example.com, and setting the HTTP location: field in the response to http://www.example.com. Google for "301 versus 302" to see when you should use either.
Note that this is an HTTP feature, not something provided by DNS... some domain name providers however do provide HTTP redirect facility for you if you use their name servers.
Note you can test this out for yourself. Go to http://web-sniffer.net and type yahoo.com (or microsoft.com or any major web site). You'll see that they respond to a redirect with the www version of the name (in less common cases some web sites go the other way, redirect www to the non-www version of the name).
When I had the misfortune to work on Microsoft's web stack, I used ISAPI_Rewrite to force www. prefixing (among other things).
At my web site I just put the following into the .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^tafkas\.net$ [NC]
RewriteRule ^(.*)$ http://www.tafkas.net/$1 [L,R=301]
For Apache based server you can use this code
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
Explanation: The first line just turns the rewrite engine on. The second line of code is to check the url entered by the user. The bash sign (!) means not equal to. The carrot sign (^) means the beginning of the statement to be checked. Backslash dot (.) refers to the special character of dot, which when used without backslash means 'anything'. The dollar sign at the end of the statement refers to the end of the url. The third statement states that if the input given by the users is not the one as given in statement two, then proceed as follows. (.*) means that anything and any number of times. This is used so as the save anything written by the user after yourdomain.com/ gets stored in the variable $1. R=301 refers to the permanent redirection, and L states that this is the last in the series of status codes.
As stated adding the WWW is the opposite direction to be going. WWW is a redundancy these days.
You can check out http://no-www.org/ for further information.
This is actually backwards. It was how they did it in 1998, but now you're supposed to if anything drop the www.
Also, example.com is specifically reserved for posting example urls. Someone probably actually owns and uses the one you provided. How do you know you're not pointing someone to a porn or malware site with that random example?

Domain redirect Problem

I have a website say domain.old hosted with say 'host-old'
I want to do away with 'host-old' and go with 'host-new'
(so effectively 'host-old' hosting would end)
Also I want a new domain - say 'domain.new'
So now I have 'domain.old' , 'domain.new' and 'host-new' with me
Now I want all my old links are preserved:
viz. http://domain.old/cat1/link1/page1/
redirects to http://domain.new/cat1/link1/page1/
Now please advice what would be the best way to go to set up with the new host.
This is a multi-step process:
Create all the pages at 'host-new' so that 'http://domain.new/cat1/link1/page1/' all work.
Enable mod_rewrite in Apache on 'host-new', and configure as below.
Change the dns entries for 'domain.old' to point to 'host.new'
wait 2-3 days for dns entries to propagate to remainder of the internet.
stop hosting at 'host-old'
The mod_rewrite config you need is:
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.old$
RewriteRule ^(.*)$ http://domain.new/$1 [NE,R=301,L]
i don't know exactly how this would work but you could parse the URL from the old site and redirect to your new host. You need a common script in your domain.old masterpage that does this for you.
The question would be easier to answer if you specified your platform. If you are on Apache, I'd take a look at mod_rewrite
You'll probably want to provide a 301 (moved permanently) http response.
When you get rid of the old domain, you can't control it any more, and therefore won't get traffic from it. You should transfer the old domain to your new host (not too hard), and then have a redirector running on it - the way other answers suggest.

Resources