Show website from server2 keeping url from server1 - .htaccess

I have a server1 (at domain1.com)
I have a website on a server2 (at domain2.com)
I need to show contents from server2 but showing domain1.com URL in address bar.
Is there a way to do it using .htaccess/mod_rewrite (or other solutions) from server1?

If mod-proxy is enabled on your server1 you can use the following code to rewrite requests from Server1 to server2 :
RewriteEngine on
RewriteRule ^(.*)$ http://server2.com/$1 [P]

Related

Map domain name to a space on the server

I have a domain, e.g., domain.org and a hosting space on the server that has the address http://server/node/myname.
How I can map domain.org to http://server/node/myname such that when I visit to domain.org/some/path I go to http://server/node/myname/some/path?
The domain service offers me web forwarding with HTTP/301 Forwarding or Cloaking Forwarding but none of them works for me. They simply redirect me to http://server/node/myname when visiting domain.org/some/path.
If you have access to apache config It would be Directory config within your VirtualHost config part. see https://wiki.ubuntuusers.de/Apache/Virtual_Hosts/
You can write redirects within your site like here:
https://en.wikipedia.org/wiki/URL_redirection
Modify your .conf file if you have access to Apache configuration like this...
RewriteEngine on
RewriteRule ^(.*) http://server/node/myname/$1 [P]
This will redirect all traffic for the base URL you set above this in that configuration file.
If you already have a rewrite rule in place for domain.org you can use scripting within those pages to do simple URL redirects by parsing the URL on domain.org into a new string for http://server/node/.

Redirecting web content but not the name of web page address

I have two internet addressess, say one.com and two.com
The content of my webpage is all under the domain one.com. If I hit address two.com I want it to redirect to one.com but still with address two.com. For example if I type in address bar two.com/article. I want to still show this same address but the content displayed would be as from address one.com/article
I tried to use htaccess file, but still no luck.
Any advice would be appreciated.
You can't achieve that while performing an external redirect.
If both domains are hosted on the same server, then you can perform an internal redirect (e.g. using Apache's Alias, AliasMatch or mod_rewrite).
If the domains are hosted on different servers, then you would have to proxy one of them. You could do this with ProxyPass from Apache's mod_proxy.
Go to your domain name provider and create an alias (CNAME record) for the address.
You will need to enable mod_proxy in your Apache config for that. Once mod_proxy is enabled, enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory on domain2.com host:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [NC]
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [L,P,NC]

Masking sub domain with a new domain while preserving the paths

I own a domain since long, just masking the names:
http://mydomain.com
Later I started using a subdomain on this domain for some project.
http://subdomain.mydomain.com
Those projects grew and now I have a structure like
http://subdomain.mydomain.com/project1
http://subdomain.mydomain.com/project2
http://subdomain.mydomain.com/project3/subproject1
http://subdomain.mydomain.com/project3/subproject2
http://subdomain.mydomain.com/project3/subproject3
http://subdomain.mydomain.com/project4
....
etc.
now I bought a new domain (shortdomain.com) where I plan not to move anything but everything should be accessible via redirects so everything looks like:
http://shortdomain.com
http://shortdomain.com/project1
http://shortdomain.com/project2
http://shortdomain.com/project3/subproject1
http://shortdomain.com/project3/subproject2
http://shortdomain.com/project3/subproject3
http://shortdomain.com/project4
...
etc.
So basically I need to do two things:
1. if anyone visits my old domain, redirect them the new naming structure. i.e. if someone loads http://subdomain.mydomain.com/project2 they should be redirected to http://shortdomain.com/project2
when a user loads/redirected to http://shortdomain.com/project2 this should actually load the content present at http://subdomain.mydomain.com/project2
So I will not manually migrate projects,codes and GBs of other data. I think this might be acievable by smart redirection only.
Just FYI:
1. I have full DNS control of both the domains
2. I am hosted on hostgator
3. I use cloudflare on the first domain and would like to continue using it
I think this might be acievable by smart redirection only.
No, redirection changes what's in the browser's location bar. If you redirect to shortdomain.com then the request will get sent to shortdomain.com, and have nothing to do with subdomain.mydomain.com anymore. If you redirect back to subdomain.mydomain.com, then the location bar in the browser will change as well.
What you really want to do is point shortdomain.com to the same server and document root that subdomain.mydomain.com is on. Then use this to redirect (either in htaccess file or server config):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://shortdomain.com/$1 [L,R=301]
If, for whatever absurd reason you can't point the shortdomain.com DNS to the same webserver that serves subdomain.mydomain.com, or can't setup that webserver to accept requests for the shortdomain.com host, you need to setup a proxy server. And it'll work something like this:
2 Webservers, server A (hosts subdomain.domain.com) and server B (hosts shortdomain.com)
Someone requests http://subdomain.mydomain.com/project3/subproject1
server A gets the request and redirects the browser to http://shortdomain.com/project3/subproject1
browser's location bar changes to new location
server B gets the request and reverse proxies the request back to server A
server A gets the request again but must recognize that it is a proxy and then serve the page instead of redirecting
As you can see, this is a horrendously ineffecient solution. It's also a high possibility that your hosting service won't allow you to setup proxy servers.
I have full DNS control of both the domains
With full control I assume you can enable mod_proxy as well on Apache web-server of shortdomain.com. Once that is done set it all up this way.
On subdomain.mydomain.com enable mod_rewrite and place this rule in Apache config OR DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^ http://shortdomain.com%{REQUEST_URI} [L,R=301]
On shortdomain.com enable mod_proxy, mod_rewrite and place this rule in Apache config OR DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^shortdomain\.com$ [NC]
RewriteRule ^ http://subdomain.mydomain.com%{REQUEST_URI} [L,P]

Detect if server is redirecting via a .htaccess file

I've created a .htaccess file to redirect a specific IP address to another file:
# Allow rewriting of htaccess
RewriteEngine On
RewriteBase /
# Get remote address/block of addresses to redirect
RewriteCond %{REMOTE_ADDR} 127\.0\.0\.1
# Define the file being accessed
RewriteCond %{REQUEST_URI} /index\.php$
# When the remote address(es) try to access the file above redirect them to the file below
RewriteRule .* /other-index\.php [R,L]
This works well and the above redirects localhost to other-index.php if it tries to access index.php but allows all other remote hosts to access index.php.
The remote host is calling: http://www.example.com/index.php
My question is can the remote host (in this case localhost) find out that it is being redirected by the server?
My question is can the remote host (in this case localhost) find out that it is being redirected by the server?
Yes because the client (at 127.0.0.1) makes a request for /index.php to the server. The server responds with a 302 redirecting the client to a different location /other-index.php.
Normally this all happens seemlessly, like if you are using a browser. The browser gets the new location (/other-index.php) and simply makes a new request to the server for the new location and gets it. It happens instantly. But the remote host (the client) knows it's being redirected because the first request it made (for /index.php) resulted in a 302 with a new location (/other-index.php).
If the R flag in the square brackets of your RewriteRule was omitted, there wouldn't be a redirect and the URI would be internally re-written by the server, and the client (the remote host) would be oblivious to the fact that was served the content at other-index.php when it requested other.php.

Showing page from different domain for certain url?

I have a page on my site, mysite.com/test, that I want to load a page on another site, test.jit.su, for example. I had set up my nameservers with my registrar (godaddy) so that I could add an A record for mysite.com/test that would point to test.jit.su. This has posed a few problems with email and other things on my webhost, so I'm going to change the nameservers to those of the webhost.
Is there still a way to do this without the A records?
DNS records can only set an alias on a domain (of any level), e.g. mydomain.com --> yotherdomain.com
which also automatically implies redirects like
mydomain.com/anything --> myotherdomain.com/anything
but you can not do mydomain.com to one server and mydomain.com/anything to another server, if you want to do this you'll have to use HTTP redirect
Changing A record will not resolve your issue as noted by #unleashed-dev.
You will need to enable mod_proxy in your Apache config for that. Once mod_proxy is enabled, enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under $DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# proxy /test to http://test.jit.su/test
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(test)/?$ http://test.jit.su/$1 [L,P,NC]

Resources