How do I redirect two domains to another domain with Apache? - apache

I have three domains, two of which are supposed to be redirected to the other.
www.example.com
www.example.net
www.example.org
I already have the DNS entries setup so that they all will go to the same IP address.
What I want to have happen is for the .com and .net urls to be permanently redirected to the .org address. So:
http://www.example.com -> http://www.example.org
http://www.example.net -> http://www.example.org
http://example.com -> http://www.example.org
http://example.net -> http://www.example.org
In my .htaccess file I have the following configuration which I setup from the best of my understanding of http://httpd.apache.org/docs/2.2/rewrite/remapping.html#canonicalhost
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*)$ http://www.example.org/$1 [L,R=301]
Theoretically, what should happen is that any requests to the site where the HTTP_HOST is not www.example.org, then it should be permenantly redirected to http://www.example.org/ followed by any original path that was on the URL.
I'm sure this is easy to do and I'm just missing something obvious, but it seems like all of the other questions and search results talk about redirecting subdomains and file paths, but none of them talk about redirecting a top level domain in a URL.

Thats almost the same that I use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$
RewriteRule ^/(.*)$ http://www.example.org/$1 [R=301,L]

Turns out I was on the right track. My final code wound up being this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$ [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [L,R=301]
The root of the problem for me was that my host was unaware I had multiple domains. So when requests would come into the site after being resolved, the host would throw up a page saying there it couldn't find a site. So I added my example.com and example.net sites to my host and parked them to example.org.
Perhaps somebody else can better explain what happened here than I, but the real issue was not with the rewrite but with my hosting provider.

Related

Why is the htaccess in my subdomain folder overriding aspects of the htaccess in my main domain?

Let me explain my setup here, I have two domains, for the sake of naming them lets call them domain1.com and domain2.com. I have shared hosting that runs cPanel.
Domain1.com is my main domain, and is what I have my hosting account setup using. I have a website hosted at that domain. Domain2.com is set as an add-on domain, and directs to a folder inside of the main domains root. That has it's own website (a Ghost blog).
That all works fine. However the websites hosted at the two domains are quite different and I did not want the subdomain to work, but as far as I can tell add-on domains in cPanel have to have a subdomain in order to be added to the account. The redirect options for the subdomains in cPanel aren't good enough for me, as I wanted anyone accessing say domain1.com/domain2 to get a 404 error as if it didn't exist. So I set up the .htaccess file for domain1.com to look like this:
RewriteEngine On
#301 (permenant) redirects all HTTP requests to HTTPS (SSL)
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#404 redirects all domain.com/subdomain requests
RewriteCond %{HTTP_HOST} ^(www.)?domain1.com$ [NC]
RewriteCond %{REQUEST_URI} ^/domain2/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
#404 redirects all subdomain.domain.com & www.subdomain.domain.com requests
RewriteCond %{HTTP_HOST} ^domain2.domain1.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.domain1.co.uk$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ - [L,R=404]
This worked perfectly, anyone tryting to access domain2 via domain1 would get a 404 error. But that was before I installed Ghost on domain2 After getting Ghost installed I created an .htaccess file in the domain2 folder inside of domain1's root.I had to put this in it in order for Ghost for to run, as it uses NodeJS:
RewriteEngine on
RewriteRule ^(.*)$ http://127.0.0.1:55555555/$1 [P,L]
This also works perfectly except for one aspect. Now all requests to the domain using domain1.com have started working again. For example if I type domain2.domain1.com it will now show me the Ghost blog where as before it was correctly displaying a 404 Not Found error. Why is this and how do I go about rectifying this issue?
As a side note, I also started trying to have all http requests redirect to https. I added the following to the domain1.com htaccess file as I wanted all requests on both domains to redirect to https and I assumed this was necessary:
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now this kind of works. All requests, whatever they are (with or without www for instance), redirect to https, and when I visit it in my browser I get a green the icon indicating it's secured with SSL. Again even with this the previous issue of the domain redirect still doesn't work. But I have another odd issue here.
If I type in my browser simply "domain1.com" (ie without https:// or www. or a combination of the two), it will show just "domain1.com" in the address bar but with the green icon and say it is secured. If however I type in "domain2.com" (again with no https:// or www etc.) it does the same thing except it states it is not secure, suggesting it requested http.
And yet if I type in www.domain2.com or http://domain2.com, that works and I get the secured icon. It is highly puzzling. It seems that the https redirect works on every request except for just "domain2.com" while any other variation (www.domain2.com, http://domain2.com etc) works fine. Any clues? I mean clearly I've done something wrong with the htaccess files but I don't know what, I got most of what I'd put in them from searching sites like Stackexchange, but personally I don't really know anything about them or how they work.
I think I have fixed my own problem here.
I corrected the main htaccess so the https redirect acts like this;
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
From what I can tell it's better to do it this way than the way I had it previously. This in itself didn't fix the problems. What fixed it for me is editing the domain2.com htaccess to look like this:
RewriteEngine on
#Force WWW if it isn't in the request
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
#Or include WWW even if it is in the request
RewriteCond %{HTTP_HOST} ^www\. [NC]
#Then Force it to use https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Defines this domain so main htaccess rules for subdomains work
RewriteCond %{HTTP_HOST} ^(.*)?domain2.com$ [NC]
#Proxy for Ghost
RewriteRule ^(.*)$ http://127.0.0.1:55555555/$1 [P,L]
Now everything works. All requests are redirected to https. All requests to the subdomain from the main domain fail with a 404. domain2.com now redirects to https://www.domain2.com or https://domain2.com so it is now always secured by SSL. I'm a novice at htaccess as is probably evident and I've no doubt some or all of this could have unnecessary elements removed or simplified so if anyone wants to correct my syntax so it's less messy I'd appreciate it.

Subdomains that don't exist are indexed by Google - Zombie subdomain removal via .htaccess

I'm experiencing an unusual issue where Google is indexing subdomains of my site that don't exist. It looks like these 'subdomains' are created from misspellings such as w. ww.. There's even some instances of http://abc.www.example.com being indexed.
My main concern here is with content duplication in the SERPS as there are several of these non existent / zombie subdomains indexed by Google. I want these subdomains to either drop out of the SERPS or be redirected to the secure www. version of the website.
I'm experienced in web development, but server configuration and .htaccess / mod Rewrite rules are not my biggest strength.
There's currently a rewrite rule to force https across the site - could this be amended to force the zombie subdomains to redirect to https://www.? If it is possible, can I ensure it doesn't effect real subdomains of the site?
Current .htaccess redirect :
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,R=301,L]
To address the comments... You can't block hostnames (subdomains) in robots.txt if all subdomains are pointing to the same place (the same site), unless you internally rewrite those requests to a different robots-block.txt file. However, robots.txt doesn't necessarily prevent indexing, it prevents crawling, and if these subdomains are already indexed, then they are going to remain indexed for while if you simply block crawling.
Are you on a shared server? Or do you have your own? This will only be a "default host" issue if this is your own server and your site is the first (or only) host defined on it. (This is rare to be a shared server issue.) Otherwise, this sounds like you have a "wildcard" subdomain defined in DNS (and corresponding ServerAlias in the server config).
Remove the wildcard subdomain and it will resolve your issue.
Otherwise you can redirect (or block) any non-canonical hosts in .htaccess. For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule ^ - [F]
The above would send a 403 Forbidden for any request that is not for example.com or www.example.com.
Aside:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,R=301,L]
This is not correct. It only canonicalises http://example.com. It won't canonicalise/redirect http://www.example.com or https://example.com since the two conditions are implicitly AND'd. You need to OR the two conditions:
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{SERVER_PORT} 80
In order to match example.com or http.

Redirection between domains

I have two webs on two different domains. One is on domain.com and the other web is on domain.es.
I am shutting down the web on domain.es but first I have to redirect every request to domain.es to domain.com. This is easy and I had already done it with RewriteCond & RewriteRule, like this:
www.domain.es/ -- redirects to --> www.domain.com/
But I am having a problem now. I also need to redirect one specific page from domain.es to domain.com like this:
www.domain.es/a_page/ -- redirects to --> www.domain.com/another_page/
I am having problems when redirecting with both rules, as when apache detects a request to www.domain.es/a_page it goes to www.domain.com/ and not to the specific page I need.
I have this in my virtualhost:
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
RewriteRule ^(.*)& http://www.domain.com/another_page/ [R=301,L]
RewriteCond %{HTTP_HOST} ^domain.es/
RewriteRule ^(.*)& http://www.domain.com/ [R=301,L]
I've got the [L] flag so apache would stop processing rules, but this is not working.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
The line above is never going to match anything - you're asking if the host is equal to domain.es/a_page/, which is a host as well as a path. You need to test against only the hostname, and then redirect based on the path, e.g.
RewriteCond %{HTTP_HOST} ^domain\.es
RewriteRule ^a_page/ http://www.domain.com/another_page/ [R=301,L]

.htaccess redirect to example.com except for directory redirect to example.com/example

Variations on this question have been asked, and so I apologize in advance -- I simply cannot get this to work. I know the devil is in the details with the .htaccess, so I'm sure I'm missing something small.
Here's what have:
I have a page: example.com/ which redirects all traffic to its https equivalent. I'm building the next version of the same site inside of the directory example.com/example.com/ I'm trying to write a redirect that will send all traffic to its https equivalent except for the directory that I'm working in, which should be left alone. My problem is, whenever I try to access example.com/example.com/ I get redirected to the 404 of example.com (which makes sense). I just can't get the redirect to not loop indefinitely.
I currently have a redirect from http to https for example.com as follows:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
This works just fine, and only causes troubles when I try to access the subfolder.
tl;dr
Here's what I need:
http://example.com/ REDIRECTS TO https://example.com/
http://example.com/[anything else] REDIRECTS TO https://example.com/[anything else]
https://example.com/example.com/ SHOULD NOT REDIRECT
Try:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !example.com
RewriteRule ^(.*)$ https://example.com/$1 [L]

Redirect all requests to HTTPS AND www to non-www

I'm fixing my Apache (2.4.12) config files on a server that serves three different domain names. I have a different config file for each site. I cannot for the life of me figure out how to accomplish both of the following:
Redirect all http requests to https, keeping the entire rest of the request (subdomain/host AND document path) exactly the same
Redirect all www requests to non-www
I've read that this can be done in one step if I have only one *:80 VirtualHost and put the rewrite rules there (the remainder of my subdomains are all *:443 VirtualHosts with the exception of www), but I can't figure out how to do it. These answers on SO did not work:
The accepted answer in this question is not correct (only does the https redirect)
This answer does not work for me--only the https redirect works.
This question doesn't deal with a wildcard subdomain and is thus inapplicable.
This question is also inapplicable because it doesn't deal with subdomains.
EDIT: This is the code I reference in the comments for mike.k's answer below.
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*)$ https://example.com/$1 [R=permanent,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com%{REQUEST_URI}
</VirtualHost>
This is from my production system and works.
THE_HOSTNAME is for instance server, and then THE_FQHN is server.domain.edu, which helps for SSL certificates if you don't want to support wildcards and multiple domain names.
# redirect to FQHN
RewriteEngine on
RewriteCond %{HTTP_HOST} THE_HOSTNAME$
RewriteRule ^(.*)$ https://THE_FQHN/ $1 [R=permanent,L]
# redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://THE_FQHN%{REQUEST_URI}
In your case www.domain.com would be where THE_HOSTNAME is, and THE_FQHN would be domain.com, just flipped around