Redirecting all subdomains to a domain - apache

I am having an issue with one of my sites where I need to redirect all subdomains to the main domain name. I have a Drupal site set up that shares content between 2 domains (using the Domain module). Here is basically what is going on.
In my .htaccess file I have these rules..
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
This redirects any subdomain to mydomain.com, which is great!
The problem is that I have another domain (myotherdomain.com) which uses the same Drupal site to share content with via the Domain module.
With that .htaccess rule in place when I go to myotherdomain.com it is redirecting to mydomain.com which I do not want to happen. Is there any way that I can stop that from happening?
To recap:
anything.mydomain.com needs to redirect to mydomain.com
any traffic to myotherdomain.com needs to stay at myotherdomain.com and not get redirected.

Any RewriteCond before a RewriteRule will be applied. Have you tried adding simply RewriteCond %{HTTP_HOST} !^myotherdomain\.com$ [NC]?

Just add another exclusion condition for your other domain:
RewriteCond %{HTTP_HOST} !^myotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
This way, any request for "myotherdomain.com" won't get redirect, and any request for "mydomain.com" won't get redirected.

Related

Redirect only HTTP subdomain to HTTPS subdomain in htaccess

How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would create a redirect loop (if executed at all). In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop).
These directives would need to go in the .htaccess in the document root of your subdomain. Or at the top of your root (WordPress) .htaccess file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.
This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This checks that HTTPS does not contain "on" and the subdomain is being requested before redirecting to HTTPS on the same host.
Although if WP is simply redirecting the main domain, then you could do all this in .htaccess and simply remove the condition that checks against HTTP_HOST. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example:
RewriteCond %{HTTP_HOST} ^((subdomain|www)\.)?example\.com [NC]
Please try one of the followings:
you need to edit
Redirect "/" "https://yourwebsite.com/"
OR
you dont need to edit following
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

Redirect a.com/a.html to b.com/a.html

I need to redirect a.com/a.html to b.com/a.html htaccess Apache. I have several subdomains that are not being redirected to the b.com(which is fine). and the example below is only the first domain that needs to be redirected. Three other domains are needed to be redirected to b.com as well with all of their subpages as a.com/a.html to b.com/a.html. Any hints?
RewriteCond %{HTTP_HOST} ^(www\.)?nexus21\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^tvlift\.com$ [NC]
RewriteRule ^ http://www.tvlift.com%{REQUEST_URI} [L,NE,R=301]
I would do each subdomain redirect separately. That way it is cleaner and more maintainable. If you are redirecting few known files then name them directly.
RewriteEngine on
RewriteRule "^/foo\.html$" "bar.html" [R]
If there are lots of file or you will be adding in future then use a pattern
RewriteEngine on
RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
See this page
http://httpd.apache.org

Subdomains Display The Wrong Content Using HTTPS

I am using a wildcard SSL on my main domain and a few sub-domains (apps, dashboard, support, training). The problem I am facing is that when I try to access a sub-domain via HTTPS, it shows the index/main page of the root domain, but when the sub-domain is accessed via HTTP, it shows the correct page. I would like to know what information I would need to put in the .htaccess file to make the sub-domains:
Automatically use HTTPS.
Show the correct content on that sub-domain when using HTTPS.
The sub-domains don't currently have an .htaccess file, because I have no idea what should be put in there at this point.
Here is an example of the HTTP sub-domain: http://goo.gl/Mcg66l
Here is an example of the HTTPS sub-domain: http://goo.gl/Aw9yl6
Here is the main domain: http://goo.gl/TfLjuy
The main domain is doing exactly what I want it to do (www to non-www and automatic https redirection), but the sub-domains are not doing what I want them to do, as described above.
Here is the .htaccess file on the main domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
Lastly, I am using cPanel under a reseller account serviced by HostGator.
Thanks for taking a look at this problem!
I had the same exact issue, also using a HostGator account. I found the answer on another question that you can find here.
They go about solving it by applying HTTPS for the main domain, forcing HTTP on a subdomain, and then as a bonus remove all WWW's
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]

htaccess subdomain redirect in infinite loop

I have a domain and also a subdomain which points via dns to the domain.
We want request at subdomain.domain.com to point to /a/b/page while keeping the subdomain.
eg: subdomain.domain.com goes to subdomain.domain.com/a/b/page
It currently is in an infinite loop.
RewriteCond %{HTTP_HOST} subdomain.mydomain.com
RewriteCond %{REQUEST_URI} !^/a/b/page$
RewriteRule (.*) http://subdomain.mydomain.com/a/b/page [R=301,L]
We are using apache2 and modx as the cms.
According to your example:
subdomain.domain.com
goes to (Redirects to, is rewritten as)
subdomain.domain.com/a/b/page
RewriteCond %{HTTP_HOST} ^subdomain.*$
RewriteRule (.*) http://subdomain.mydomain.com/a/b/page [R=301,L]
There is no need to check the URI as there is NONE.

.htaccess for domain change with subdomains - apache + plesk

We're using a plesk based system and just recently changed domain names. I'd like to redirect all requests coming in to the old domain to the new. There are many question asked in a similar fashion but mine is a bit different. I'd like to ensure that all subdomains get routed to the same subdoamin on the new domain. I set up a generic htaccess in the docroot but for some reason it is also applying to all subdomains.
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
How can I make this more general so that subdomains also appropriately get routed? For bonus points, how can I route https requests to https and http to http.
I'd like to add that the rule transforms the first url to the second which is not desirable:
http://SUBDOMAIN.olddomain.com/somepath/somefile.php
http://newdomain.com/subdomains/SUBDOMAIN/httpdocs/somepath/somefile.php
The proper transform should create the following url:
http://SUBDOMAIN.newdomain.com/somepath/somefile.php
Lastly, this should work with wildcard subdomains.
Thanks in advanced!
Try:
# ignore "www."
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://%1.newdomain.com/$1 [L,R=301]
You'd want this before the rule that you already have.
And since you've edited your question, you want an additional internal rewrite for handling subdomains in general on your new site, this has nothing to do with the redirect. This is brand new functionality that is outside of the scope of a 301 redirect. You're going to need special rules to handle internal routing of subdomains:
RewriteCond %{REQUEST_URI} !^/subdomains/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.newdomain.com$ [NC]
RewriteRule ^(.*)$ /subdomains/%1/httpdocs/$1 [L]