Redirect multiple domains to subdirectories - apache

I have multiple domains pointing to my VPS (Virtual Private Server). What I want is, for example, if I write: www.domain1.com, redirect to www.domain.com:8080/app1, because I have apps with Tomcat. The same with www.domain2.com, to www.domain2.com:8080/app2, etc.
Can I do that with htaccess?

I solved my problem with:
RewriteCond %{HTTP_HOST} ^www.domain1\.com$
RewriteRule ^(.*) http://www.domain1.com:8080/app1$1 [R=301,L]
in .htaccess, and the same with the other.

Related

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.

How to redirect www.mydom.com to mydom.com

I have a dedicated server with WHM/cPanel and hosting multiple domains on Linux.
For one of my domains, I'd like to get all http://www.mydom.com traffic to automatically go to http://mydom.com
This needs to happen for all subdirectories, files etc. e.g. http://www.mydom.com/aFile.php should go to http://mydom.com/aFile.php
Other subdomains such as img.mydom.com should still go to img.mydom.com and not be redirected.
How can I accomplish this? Thx.
You can setup redirection code in your .htaccess file to redirect your all traffic from www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [nocase]
RewriteRule ^(.*) http://example.com/$1 [last,redirect=301]

How do I redirect two domains to another domain with 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.

How to redirect from one port to another?

I've a domain on which I'm running two different web applications at different ports.
A Java web application on http://example:8888/foo
A PHP website on http://example:8080/bar
The requirement is, if an user tries to access the root of http://example:8888 or http://example:8080, then the user should be redirected to http://example:8888/foo.
How can I achieve this requirement?
mod_rewrite can achieve this easily, you will be using 301 redirects
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^/$ http://example.com:8888/foo [R=301,L]
Make sure this is added in the vhost config section of both sites.

htaccess and removing sub-domain from HTTP_HOST

I've got a wildcard DNS record, allowing all forms of *.domain.com, additionally I'm pointing several different domains at the same machine and using rewrite rules to direct requests to sub folders depending upon the url the request originates.
For instance domain1.com point to /sites/folder/domain1, domain2.com points to /sites/folder/domain2
My problem is, that i'm using the Apache var HTTP_HOST in this rule, which includes the sub domain so that sub1.domain1.com points to /sites/folder/sub1.domain1.com and with several hundred sub domains for each domain there is no way to create all those folder possibility.
My question: how to remove all sub-domains from the HTTP_HOST variable? This is what the ruleset looks like thus far:
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
Which works, until a sub domain is included in the URL...
To limit your rule to just mydomain.com (ie no subdomains) add a RewriteCond directive before the Rewrite as below
RewriteCond %{HTTP_HOST} ^[^\.]+\.com$ [NC]
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
Edit:
Allow any domains, but no subdomains
I think what he wants is to use the variable HTTP_HOST in a rewrite, but NOT with a subdomain:
HTTP_HOST = www.example.com
Rewrite rule:
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
is, in actuality:
RewriteRule ^file.xml$ sites/example.com/file.xml [L]
but NOT:
RewriteRule ^file.xml$ sites/www.example.com/file.xml [L]
-Brian