How to show a content of one website in another website? - .htaccess

I have a website for example: https://test1.com, and a page on that website https://test1.com/show-content.
I want that page to display the content from another website, for example https://test2.com/show-different-content.
I know I can do it with PHP and file_get_contents, but I'm trying to do it with .htaccess, since I understand it can be possible. I've looked through all the SO questions I found regarding that, but I found no clear solution.
What I have tried in .htaccess is the following:
<IfModule mod_rewrite.c>=
RewriteEngine On
RewriteRule ^show-content$ https://test2.com/show-different-content [P]
</IfModule>
What am I doing wrong? Or am trying to do something that is not possible?

You have two options for this if the owner of that second site actually grants permission to proxy his sites content. You can use the proxy module available for the apache http server.
Either direct:
ProxyPass /show-content/ https://test2.com/show-different-content/
ProxyPassReverse /show-content/ https://test2.com/show-different-content/
Or embedded in the rewriting module:
RewriteEngine On
RewriteRule ^/?show-content/(.*)$ https://test2.com/show-different-content/$1 [P]
Obviously the proxy module needs to be loaded and activated inside the http server.

Related

Showing content from another server on a domain without changing URL

I have seen a few similar questions on here, but none of the answers have seemed to get me where I need to be.
I have a site, say:
www.first.com
And another site called:
www.second.com
The second.com domain is on a different server than first.com, but they are for the same company. I am trying to merge the domains into www.first.com so that end users only see first.com for this particular brand.
There is a Java applet running on www.second.com/applet that needs to stay on its own server. However, I would like to only have one domain to access the contents on both www.first.com and www.second.com -- I have looked into a 301 redirect, but I am trying to only have ONE SINGLE domain for this. The problem with a 301 is that the URL changes and displays "www.second.com" in the domain, which is what I am trying to avoid.
I would like to have JUST www.first.com and if someone goes to "www.first.com/applet" it shows the content from "www.second.com/applet" but KEEP "www.first.com/applet" in the URL bar, but this is seemingly difficult to achieve with my level of knowledge.
I have tried this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?first\.com$
RewriteRule ^(.*)$ http://www.second.com/$1 [P]
to no avail. I get the following error:
Bad Request
Your browser sent a request that this server could not understand.
Size of a request header field exceeds server limit.
X-Forwarded-Server
/n
Apache Server at * Port 80
I have read a little about a reverse proxy or something that may work.
How would I properly configure this? I found this:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://example.com
ProxyPassReverse / http://example.com
ServerName sub.example.com
</VirtualHost>
From here: How to show content from other domain without changing the URL
But I am not trying to use a subdomain. Hopefully I have explained what I need here. Basically, merging two domains that need to remain on separate servers but be delivered from a single domain.
Thanks!
It is very easy:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^Domainyoustayat.tld
RewriteRule ^(.*) http://Domain-you-want-to-show-on-above.tld/$1 [P]

Linking one domain to another domain with htaccess

I'm stuck with htaccess redirect on this case:
I have myapp.com domain where my main website and service runs on. My customers logs into their accounts on myapp.com and use. Now, I am going to provide one of the features on a separate domain, let's assume "goto.com". However, I don't want to build a separate app on goto.com. I just want to redirect all coming requests to goto.com to a php script under myapp.com but this redirection should be in the backend (masked), not a 301 redirection.
Let me clear up:
myapp.com - /var/www/vhosts/myapp.com/httpdocs/index.php
goto.com --> masked redirection --> myapp.com/goto.php?$1
How can I do this with htaccess? Any suggestions?
Just found that it can be done with redirect [P] (proxy) method: RewriteRule ^(.*)$ http://myapp.com/public_view/$1 [P] What do you think? Is this a good and stable method?
That method is fine, it utilizes the mod_proxy module. The only thing I can suggest is adding the L flag as well in case you have other rules in your htaccess file.
A limitation with using the P flag in an htaccess file is that if a page or request to http://myapp.com/ redirects, then you're URL address bar will say http://myapp.com/ instead of your other domain. The only way around this is to use ProxyPassReverse but it doesn't work in an htaccess file. You'd need access to vhost config:
ProxyPass / http://myapp.com/public_view/
ProxyPassReverse / http://myapp.com/public_view/
Additionally, if http://myapp.com/ sets cookies, you'll need to use the ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath directives to rewrite those.

URL masking (?in .htaccess) from one domain to another

I've been searching the archives but I can't find anything that is making too much sense to me.
I have a site with a couple of subdomains which redirect to other sites.
E.g.
the visitor types - www.jmp.redtwenty.com.au - and is redirected to - http://creator.zoho.com/redtwenty/jmp-conversion-tracking
Is there any way to mask this redirect so that the visitor still sees jmp.redtwenty.com.au in the address bar?
I keep seeing mention of a rewrite rule in .htaccess but not sure if that is what I want.
Thanks
Mike
You can do this a few ways, but you'll need to make sure mod_proxy is enabled.
If you have control of the server config or the vhost config of the www.jmp.redtwenty.com.au/ domain, you can add this to it:
ProxyPass / http://creator.zoho.com/redtwenty/jmp-conversion-tracking/
Or in the htaccess file in the document root of http://www.jmp.redtwenty.com.au/:
RewriteEngine On
RewriteRule ^(.*)$ http://creator.zoho.com/redtwenty/jmp-conversion-tracking/$1 [L,P]

.htaccess rewrite full domain name

Are there anyway to make a user get to download thefile by let them type:download.mywebsite.com/thefile
in the browser address while the "download.mywebsite.com/thefile" does not exist but the "www.mywebsite.com/thefile" does.
I've got nearly zero knowledge with .htaccess and mod_rewrite engine. I hope my question is clear.
IF you own mywebsite.com then yes you could, you would have to place a ServerAlias in your root httpd.conf directive then use mod_rewrite to re-write all requests for download.mywebsite.com/thefile to www.mywebsite.com/thefile but the mod_rewrite would not know if a file exists or not in advance.
Another way to do it would be implement a custom error document for a 404 handler which redirects a user to a suggested URL.
RewriteEngine On
RewriteRule ^/download/(.*)$ http://download.mywebsite.com/$1 [R=301,L]
You need to set up your web server to listen for sub-domains as well

How can I redirect to a different domain without changing the URL in the address bar?

I want to redirect from:
domain1.com/photos
To:
domain2.com/photos
I want the URL in the address bar to still read:
domain1.com/photos
Is there a way to do this using only .htaccess?
Note:
My .htaccess file for domain1.com is currently completely blank.
No, there isn't a way to do this with .htaccess. Doing so would present a glaring security hole - imagine someone doing this with a bank's website!
If both are hosted on the same server, do this in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule (.*)$ http://www.domain2.com$1 [P]
</IfModule>
If you own both domain1 and domain2, you could accomplish this through domain name forwarding. Check your domain name registrar (like godaddy.com) for the options.
No, you can not do it through htaccess file.
You could open an iframe in domain1.com/photos that shows the contents of domain2.com/photos. But then the url would never change from domain1.com/photos, even when you went to a different page in domain2.
What are you trying to do? It sounds very sketchy. Do you own both domains? Why would you want to duplicate the contents of one site at another address?
Why is this not possible? Seems like a reasonable task as long as your Apache has mod_proxy installed:
ProxyPass /photos http://domain2.com/photos/
http://httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypass
A way around it, if the page at domain.com/photos is a server side script, do an HTTP call and serve up the response.
In ColdFusion:
<cfhttp url="another.domain.com/photos">
<cfoutput>#CFHTTP.FileContent#</cfoutput>
They'll be an extra request, but it'll get you the output you want.
its impossible using htaccess file.

Resources