Need to proxy to dynamic ports - linux

I use the gameserver panel pterodactyl.io. There I want to host Sinusbot, which has a web interface. Of course, all servers have different ports, and every new server gets another port. I'd like to redirect URLs like:
https://example.com/1234 to http://localhost:1234. Unfortunately, I have no idea how I can realize this with apache2 -.-

This is actually quite simple with mod_rewrite
RewriteEngine ON
RewriteRule ^/?(\d+)$ http://localhost:$1/ [P]
Header edit Location http://localhost:(\d)/(.*) http://example.com/$1/$2

Related

.htaccess redirect from IP adress to actual domain

How do I redirect IP to actual domain? For example:
123.45.678.901 to www.example.com
and all subfolders and subpages like
123.45.678.901/all-pages to www.example.com/all-pages
Also, my SearchConsole also recognizes "top linking sites" for the same IP but in this manner:
IP 123.45.678 ---------------------------- 38,132 LINKS
IP 123.45.678.901 ------------------------- 3,617 LINKS
So I probably should redirect 123.45.678 as well. Is that possible and how?
One more thing - IP opens in HTTP and domain opens in HTTPS protocol.
Your comment to your question indicates that this is much easier than the description in the question itself indicates... The following should point you into the right direction, though you may have to tweak it for your situation. It odes not implement any specific address (which might change) but instead redirects every requested host name (so also ip addresses) that do not match your desired host name. In case you want to add exceptions you can di that using RewriteCond directives, for example in case your http server serves multiple host names / domains.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [QSA,R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Here is the solution to this kind of problem
In the .htaccess file put next code:
RewriteBase /
RewriteCond %{HTTP_HOST} ^123\.45\.678\.901$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
So if you have a URL wit IP like
123.45.678.901/all/other/pages/
after implementing this code all pages are going to be redirected to
www.example.com/all/other/pages/
I hope i could help anyone because this is one of very important factors which may ruin your SEO.

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.

1 domain.. 2 server and 2 applications

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.

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