redirecting without changing URL - .htaccess

i want to redirect domain domain1.com to domain2.com completely but without changing domain1.com URL.
i thought i could do that with .htaccess but i found out that it is not possible because they are to different domains and i should use http:// in .htaccess rule so it would be an external rewriting not an internal rewriting,so the URL will change.
is there any other solution? is using iframe the only solution?
i have to add that i don't want to change DNS setting for these 2 domains.

If both domains point at the same server then you can setup your apache config to point both domains at the same document root.
NameVirtualHost *
<VirtualHost *>
ServerName domain1.com
DocumentRoot /www/mysite
</VirtualHost>
<VirtualHost *>
ServerName domain2.com
DocumentRoot /www/mysite
</VirtualHost>
However, I would recommend domain2.com redirects (with a 302 redirect in the .htaccess) because it will improve your search engine optimisation, as both sites will be considered as one. So if a GoogleBot finds domain2.com as a link in another site, it will add it as a pagerank to domain1.com.

If they run on the same webserver you could set domain2 as an ServerAlias for domain1.
I thought I once read you could give a flag to mod rewrite that it internally proxies the request to another domain, however I have never used this myself (if it even exists) and I can't honestly say I think it'll be great for performance.. (but maybe it is..)
But why would you want two different domains show the exact same website, without changing the hostname to, what you see as, the primary one. If I might ask?

Related

VirtualHost not redirecting

I am trying to redirect http://eamondev.com:3000 to https://omniatm.eamondev.com with a VirtualHost. I am using node to serve a site to http://eamondev.com:3000. I am using vhost with node like this:
app.use(vhost('omniatm.eamondev.com', express.static('/')));
I have never used vhost and it took me a while to figure this out without having to split up all my code like I was working with more than one site (when I am not), so I'm not sure if it is exactly how it should be for an Apache redirect to work.
In my apache conf file I have:
<VirtualHost *:80>
ServerName omniatm.eamondev.com
ProxyPreserveHost on
ProxyPass / http://localhost:3000/
</VirtualHost>
I am also using WHM on a VPS, I'm not sure if this is relevant or not, but the ServerName (with protocol, what I type into the browser) needs to be https://omniatm.eamondev.com.
I cannot serve node on port 80 of my server (and then redirect to subdomain) because my main site (http://eamondev.com) is running on port 80.
I have referenced most of the stackoverflow questions about this and nothing has worked. I should mention (although I'm not sure exactly how it is relevant, I just saw it in a stackoverflow question I looked at), my hosting support (bluehost) used WHM to set things up with a wildcard ssl certificate to make the omniatm.eamondev.com subdomain https.
How do I redirect http://eamondev.com:3000 to https://omniatm.eamondev.com using apache (or vhost)?
Proxy passing as given in the question will not do any redirects instead it will retain the URL as such and proxy the content from elsewhere. In Apache configuration, we have an option to do redirects, in the bellow sample, we are checking for the host and based on it issuing an redirect to the desired URL
<VirtualHost *:80>
ServerName omniatm.eamondev.com
Redirect / https://omniatm.eamondev.com
<If "%{HTTP_HOST} != 'eamondev.com:3000'">
Redirect "^/?(.*)" "https://omniatm.eamondev.com/$1"
</If>
</VirtualHost>

Sub-domain forwarding with masking via .htaccess

My sub-domain sub.domain.com is forwarding (redirecting) to another.website.com correctly. However, I want to enable masking, meaning that after redirecting I want my sub-domain to show in the address bar rather than the other domain, while still showing the other domain's page content.
What should I put in the .htaccess file in order to perform this action?
Also, note that the .htaccess file is under my domain's file manager in cpanel and not subdomain's (maybe that's a given for you, but bear with my basic knowledge please...).
Should you need any more information do not hesitate to point it out please.
Thanks in advance!!
You should check the mod_proxy module, the ProxyPass directive and ProxyPassReverse directive. Put these directives in config file:
ProxyPass / https://another.website.com
ProxyPassReverse / https://another.website.com

Why use <VirtualHost> if you have only 1 domain name for the server ? But why can't "ServerAlias" be used w/o a <VirtualHost>

Okay so... i tried using
ServerName something.ooo
ServerAlias www.something.ooo
but it appears..
you can not use ServerAlias unless it is used within a
<VirtualHost *:80>
</VirtualHost>
Why is it like that?
What happens if the server is used only with 1 domain name and thus there is no need for "VIRTUAL" things. such as "VIRTUALHOSTS" ?
VirtualHost is designed to be used with multiple sites, however, when using an alias, Apache is assuming that you have example.com, and are wanting widget.example.com. With that said, just because it's only a www, does not mean that Apache doesn't see those as "separated" domains, thus needing a VirtualHost tag.
This is why you can use ServerName, but can't use ServerAlias without VirtualHost. WWW and non-www are separated domains.

Domain that shows another domains subdirectory

Is it possible to have e.g. "domain.com" that shows "domain2.com/dir".
What I mean is, that instead of domain.com just redirecting, I want it to show e.g. "domain2.com/dir/subdir" as "domain.com/subdir"
All this is regarding to a CMS.
What I need is, that my frontend to my CMS is at domain.com. A customer is creating his account with a shorttag. E.g. "domain2.com". Then their unique URL to THEIR frontend is "http://domain.com/domain2.com".
What I need is, that domain2.com can show "http://domain.com/domain2.com" - so that the customers doesn't need to redirect their users to http://domain.com/domain2.com, but can simply just redirect to their own domain - domain2.com
Thanks in advance.
If you're using cPanel you can set up an addon domain that can point to a specific directory.
If you're just using apache you'll need to point the domain to another folder in the vhosts.
<VirtualHost *:80>
DocumentRoot "/home/user/domain.com"
ServerName domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/home/user/domain.com/dir"
ServerName domain2.com
</VirtualHost>
you can use http header in your first page(domain2.com/dir/subdir) to redirect to second page
in php this is something like this (in index.php file in subdir):
<?php
session_start();
header('Location: http://domain.com/dir');
die();
?>

make all subdomains access website root files

Here's what I want to do:
Let's say i have www.mysite.com and it's a complex website with alot of files
I want to make fr.mysite.com, it.mysite.com, uk.mysite.com to access the file in the root website (ie: fr.mysite.com/jobs.php will actually load mysite.com/jobs.php but in the browser url it will show the link with subdomain).
I want to build a site with content from multiple countries and I want each country to be accessed with it's code as a subdomain. If I can do that I can then process the url in php and know what country code it's in the url.
Copying the entire site to each subdomain folder isn't an option.
Let me know if you have any idea on how to do that, I guess it's a .htaccess thingy but I can't figure it out .
Use ServerAlias in your VistualHost configuration: set it up as single virtual host where all sub-domains point into the same root folder:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.mysite.com
DocumentRoot "\path\to\your\site\"
...
</VirtualHost>
This is the most recommended way -- no need to involve URL rewrite here.

Resources