Remap domain.io to show as domain.com in address bar - apache

I have domain.io and domain.com BOTH pointing to the SAME server. How do I set this up so that domain.io never shows up in the address bar, and it's always domain.com?
Thanks!

You can use Apache's mod_alias Redirect directive for this:
<VirtualHost *:80>
ServerName domain.io
ServerAlias www.domain.io
Redirect permanent / http://domain.com/
</VirtualHost>
In your case, permanent (301) redirect sounds appropriate but you may want to look at HTTP redirect: 301 (permanent) vs. 302 (temporary)
Also, it's really unimportant where the domains are hosted in this case; they do not need to be on the same server. In fact, many DNS and web host providers actually provide this functionality as a service, and will host the equivalent of above configuration for you (though not necessarily with Apache; nginx is probably more likely for this purpose these days).

Related

Rewrite spare domains to main domains with .htaccess

We have a site with an English and Spanish version, each on a different domain. We also have a few spare domains for each language which we'd like to redirect to the language's main domain.
Specifically:
estadiosfutbol.net/..., estadiosfutbol.org/... and estadiosfutbol.info/... should all redirect to https://estadiosfutbol.com/...
worldfootballstadiums.com/..., worldfootballstadiums.info/..., worldfootballstadiums.org/... and worldfootballstadiums.net/... should all redirect to https://worldstadiums.football/...
I'm struggling with the rewrite rules so any help would be greatly appreciated.
There are two ways this can be done. The first is the simpliest, but is not always practical.
First Method
This method does not require HTACCESS files. In your Apache server configuration you just need to add ServerAliases for each of the domains that you want it to handle. (You must make sure all the domains are pointing at the same machine)
The Code
NameVirtualHost *:443
<VirtualHost *:443>
ServerName estadiosfutbol.com
ServerAlias estadiosfutbol.info estadiosfutbol.net estadiosfutbol.org
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:443>
ServerName worldstadiums.football
ServerAlias worldfootballstadiums.com worldfootballstadiums.net worldfootballstadiums.info worldfootballstadiums.org
DocumentRoot /www/otherdomain
</VirtualHost>
Note: This will only redirect if the user tries to access the website using SSL. (eg ) If you want it to redirect all traffic from both port 80 and port 443 you would need to make separate virtual hosts and use the second method to achieve the redirection.
Second Method
The second way is a little more complicated, but works in almost all situations. There a two main steps that need to be carried out in order for this to work properly:
Make sure that whatever server software you are using is setup to be looking for all the domains. The server has to have a VirtualHost(Apache) that is listening for each domain in order for the next step to do anything.
Create a .HTACCESS file under each domains' root that looks similar to this:
The Code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !estadiosfutbol.net$ [NC]
RewriteRule ^(.*)$ https://estadiosfutbol.com/$1 [L,R=301]
Note: You will need to change the third line on each domain to be the domain to rewrite from (eg estadiosfutbol.net/, estadiosfutbol.org/ and estadiosfutbol.info)
Note: Changing the forth line is all that is required for the separate domain.

Redirecting New Domain Name to Server

I recently purchased a new domain name from 1and1.com and used their HTTP redirect option to point to the address of my server. Let's say, for example, the fresh domain is new.com and the established server is old.com.
I have it redirecting to old.com/new via 1and1's configuration page, which works, save for the fact that when I visit new.com, it changes the browser's URL to old.com/new. This is obviously not what I want to happen.
I've set up htaccess rules:
# BEGIN New.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^new.com
RewriteRule ^(.*) http://old.com/new [P]
</IfModule>
# END New.com
Likewise, I've done the Apache configuration of Virtual Hosts:
<VirtualHost *:80>
ServerName www.new.com
DocumentRoot /www/old/html/new/
</VirtualHost>
I then proceeded to flush my local DNS cache. Yet still, it persists in changing the address bar in my browser to old.com/new. What am I missing? Does it just need time to propagate or have I misconfigured / failed to properly set something up?
You need to change the 1and1's "new.com" DNS entry to point to the same IP that "old.com" is using. While the htaccess rule (which I assume is at the new.com document root) kind of does what you want, it requires the mod_proxy be loaded, which is something I doubt 1and1 hosting allows.
What you need to do is set it up such that when you go to a site like this and do a DNS lookup for new.com, you get the same IP as when you lookup "old.com".
On old.com's server, you have the vhost setup:
<VirtualHost *:80>
ServerName www.new.com
DocumentRoot /www/old/html/new/
</VirtualHost>
which should be all you need to at least access the contents in /www/old/html/new/.

Apache Redirects in HTTPS

I have an Apache server (https://mysite.com/sub/en/). It's setup with SSL on port 443, and in the VirtualHost tags:
<VirtualHost _default_:443>
Redirect permanent /sub https://mysite.com/sub/en/
...
</VirtualHost>
Basically, when the user comes to the site, I want them to be forced to the "/en" sub-directory. However, if you try to visit https://mysite.com/sub, it doesn't redirect you to the sub "/en" directory.
I'm really not sure what is going on. I do have a similar setup in my non-ssl virtualhost tags:
<VirtualHost *:80>
Redirect permanent / https://mysite.com/sub/en/
Redirect permanent /sub https://mysite.com/sub/en
</VirtualHost>
These rules work fine. So if the user attempts http://mysite.com/sub, it forwards them to the SSL version, and the "/en" subdirectory like I want it to. But if you try the SSL version: https://mysite.com/sub, it fails to forward.
Do I have the configurations wrong? Am I not using the right redirect or rewrite rule?
To get this working the way I wanted, I ended up using a RewriteRule:
RewriteRule ^/sub$ https://mysite.com/sub/en/
This appears to have done the trick.

non-www to www cname redirect

I'm attempting to do the following
redirect non-www to www (so example.com to www.example.com) to limit the amount of network traffic resulting from browsers sending cookies for images.
I know a 301 redirect is not advisable.
Is there a way to do this using cname dns?
If you do have access to your apache VirtualHosts configuration (http.conf) then:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
IMHO that's the best way to do it. You can't put a CNAME record on a top-level domain so straight DNS isn't a simple solution.
If you want to redirect non-www to www then you should use a redirect. A 301 redirect is "permanent" so that is the best to use as it only needs to tell the web browser one time and the web browser then goes directly to the redirected content.

Using mod_rewrite to Mimic SSL Virtual Hosts?

What is the best way to transparently rewrite a URL over an SSL connection with Apache 2.2?
Apache 2 does not natively support multiple name-based virtual hosts for an SSL connection and I have heard that mod_rewrite can help with this. I would like to do something like this:
I have set up the server so that the sites can be accessed by
https://secure.example.com/dbadmin
but I would like to have this as https://dbadmin.example.com
How do I set it up so that the Rewrite rule will rewrite dbadmin.example.com to secure.example.com/dbadmin, but without displaying the rewrite on the client's address bar (i.e. the client will still just see dbadmin.example.com), all over https?
Configure a single VirtualHost to serve both secure.example.com and dbadmin.example.com (making it the only *:443 VirtualHost achieves this). You can then use mod_rewrite to adjust the URI for requests to dbadmin.example.com:
<VirtualHost *:443>
ServerName secure.example.com
ServerAlias dbadmin.example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} dbadmin.example.com
RewriteRule !/dbadmin(.*)$ /dbadmin$1
</VirtualHost>
Your SSL certificate will need to be valid for both secure.example.com and dbadmin.example.com. It can be a wildcard certificate as mentioned by Terry Lorber, or you can use the subjectAltName field to add additional host names.
If you're having trouble, first set it up on <VirtualHost *> and check that it works without SSL. The SSL connection and certificate is a separate layer of complexity that you can set up after the URI rewriting is working.
Unless your SSL certificate is the "wildcard" or multi-site kind, then I don't think this will work. The rewrite will display in the browser and the name in the address bar must be valid against the certificate, or your users will see a security error (which they can always accept and continue, but that doesn't sound like what you'd like).
More here.
There is apaches mod_rewrite, or you could setup apache to direct https://dbadmin.example.com to path/to/example.com/dbadmin on the server
<VirtualHost *>
ServerName subdomain.domain.com
DocumentRoot /home/httpd/htdocs/subdomain/
</VirtualHost>