Redirect to URL and keep it hidden - .htaccess

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.

Related

How to redirect a URL for example (www.foobar2.com) to another URL (www.foobar.com) using htaccess file

I am setting up a link which should open another website. For example, if a browser is opening up www.foobar2.com, it should open www.foobar.com without changing the URL. I found out that it can be done using .htaccess file. Currently, if I open, www.foobar2.com, it redirects to www.foobar.com but the URL also changes to www.foobar.com. But I want that URL should not change. What do I need to do?
I tried this which isn't working -
RewriteCond %{HTTP_HOST} ^www.foobar2.com
RewriteRule ^(.*) http://www.foobar.com [P]
Please help me with this.
Thanks :)
Since the http host the request is sent to is always that shown in the URL (for obviousl reasons) you need to implement a proxy feature, this cannot be done using pure rewriting or redirecting.
The apache http server offers a proxy module for this, it can be either used standalone or integrated into the rewriting module. However you want to use it you need to take care that the module is actually loaded first. It consists of two submodules, the core proxy module and the proxy_http module in this case.
Now you can implement a ProxyPass directive for the URL you want to fetch from the other host:
ProxyRequests off
ProxyPass / https://www.foobar.com
ProxyPassReverse / https://www.foobar.com
(yes, that is an off in the first line above)
This rule needs to be implemented in the http host www.foobar2.com. It will make an internal sub request to www.foobar.com for each incoming request (since it masks the root (/) and deliver the response it receives back to the originally requesting client.

Can I redirect a user to a Google Drive PDF while keeping my URL in the address bar?

If I have a user follow a link to my site such as
mydomain.com/pdf/google_token
is there a way for me to redirect them to the Google pdf
drive.google.com/file/d/google_token/view
while keeping
mydomain.com/pdf/google_token
in the address bar?
Right now I am redirecting to google successfully using
RewriteRule ^pdf/([a-zA-Z0-9]+)$ https://drive.google.com/file/d/$1/view
in my .htaccess file, but it is replacing the URL with
drive.google.com/file/d/google_token/view
Thanks.
You are not looking for a way to redirect. A redirection always changes the URL in the client, that is the whole purpose of a redirection. What you are looking for is a proxy solution, maybe in conbination with an internal rewrite. That creates a kind of mapping: the content published on that google resource is re-published through your http host.
This would be an example for such setup:
ProxyPass /google-drive/ https://drive.google.com/
ProxyPassReverse /google-drive/ https://drive.google.com/
RewriteEngine on
RewriteRule ^/?pdf/([a-zA-Z0-9]+)$ /google-drive/file/d/$1/view [END]
An alternative would be to only re-publish a section of that remote resource:
ProxyPass /google-drive/ https://drive.google.com/file/d/
ProxyPassReverse /google-drive/ https://drive.google.com/file/d/
RewriteEngine on
RewriteRule ^/?pdf/([a-zA-Z0-9]+)$ /google-drive/$1/view [END]
That set will work likewise in the http servers host configuration and you probably can also get it to work using a dynamic configuration file (".htaccess" style file). If you really need to use such a file then take care that its interpretation is enabled in the host configiration. And you definitely need to have apache's proxy module loaded. You should prefer to place such rule in the http servers host configuration though, for various reasons.
If that setup is not possible, for example because you do not have access to the proxy module, then you can implement a simple routing solution which fetches the PDF in background using for example php's cURL extension and forwarding the payload along with correct http headers to the client that sent a request to that PDF. That is usually done for resources kept locally but there is no reason why you can't do that with remote resources too.
Some additional notes:
if you only deliver documents form that google drive resource, then you probably do not need the ProxyPassReverse directive, but only the ProxyPass.
if you run into a server internal error (http status 500) using the above setup then chances are that you operate a very old version of the apache http server. You will find a hint on an unsupported END flag in your http servers error log file in that case. Try using the [L] flag in that case, it probably will work the same here, but that depends on the rest of your setup.

Need to proxy to dynamic ports

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

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).

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.

Resources