how to redirect subdomain to actual website - .htaccess

My host automatically sets up subdomains for all our hosted websites. So that zzz-thewebsite.myhosting.com is the same files as www.thewebsite.com ...
Unfortunately, somehow google has indexed the subdomains and now I probably have duplicate content.
I'd like to remove the subdomains from the index. I'm not sure the best way to do it.
I was thinking a .htaccess file that redirects zzz-thewebsite.myhosting.com to www.thewebsite.com would probably do the trick. Of course, there are subfolders involved as well.
Is there an elegant solution for this? I suppose a robots.txt might also do it but that will be in the both the zzz-thewebsite.myhosting.com and www.thewebsite.com "sites" since they are the same physical folder on the hosting.
Thank you.
Ben's answer below works great for me on non https sites but is not working for an https site. What I am using is this:
#attempt to redirect subdomain
RewriteCond %{HTTP_HOST} ^thewebsite-zzz.myhosting.com$ [NC]
RewriteRule ^(.*)$ https://www.thewebsite.com%{REQUEST_URI} [R=301,NC,L,QSA]
#https only
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#www only
RewriteCond %{HTTP_HOST} ^thewebsite.com [NC]
RewriteRule ^(.*)$ https://www.thewebsite.com/$1 [L,R=301,NC]

Setting up a permanent redirect is a preferred way.
RewriteCond %{HTTP_HOST} ^zzz-thewebsite.myhosting.com$ [NC]
RewriteRule ^(.*)$ https://www.thewebsite.com%{REQUEST_URI} [R=301,NC,L,QSA]

Related

Redirect all sub-domain directories to primary domain

I have 2 sites that were built on separate platforms. We closed one, which was located on the sub.domain.com, and want all traffic that ends up on that sub-domain to be redirected to domain.com.
I want all sub.domain.com/(anything) to be redirected to domain.com
I have only been able to make redirects that keep the directory.
I am able to edit .htaccess
So, try this:
RewriteCond %{HTTP_HOST} ^(.*)\.your\.domain$
RewriteRule ^(.*)$ http://your.domain/$1 [L]
Thank you everyone for your help. Definitely over my head with this.
I used
RewriteCond %{HTTP_HOST} ^(.*)\.your\.domain$
RewriteRule ^(.*)$ http://your.domain/ [L]
as well as
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
To help with the breaking URLS, and for SEO

htaccess help! wildcard subdomain rewrite rule to a specific folder for only some subdomains

Through a little research I came up with an htaccess rewrite rule in my development directory that takes a wildcard subdomain to its matching instance name in my development folder. I will admit I know little to nothing about this so help is really needed. So the current htaccess behaves as such:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
example
instance_folder_name.mydomain.com
redirects to
public_html/development/instance_folder_name
this works great because it allows me to have development websites in the development directory and i dont have to be constantly creating subdomains for each site.
the problem I am having is how to approach the following scenerio:
I use virtual domains as an add on component in Joomla so that several domains are being managed by one single Joomla install. So i may have the following subdomains...
client1.mydomain.com
client2.mydomain.com
client3.mydomain.com
that all need to go to
/public_html/development/client1
I guess what i need is a general rule on how to handle all wildcard subdomains, but with exceptions for client1, client2, client3 for example
You can have a separate rules for these 3 subdomains and add an exclusion condition in the older rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(client1|client2|client3)\. [NC]
RewriteRule ^((?!client1/).*)$ /client1/$1 [L,NC]
RewriteCond %{HTTP_HOST} !^(client1|client2|client3)\. [NC]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [L]

How do I create a subdomain and then have it forward to a subfolder?

Say I have the site blog.mydomain.com and www.mydomain.com. I want to make sure that if people type in blog.mydomain.com, that it'll redirect to www.mydomain.com/blog.
(I know the question is usually asked the other way around..)
I found and edited the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/blog/$1 [R=301,L]
Is this correct?
And: Do I need to create a record in my DNS to make the subdomain? (since this .htaccess stuff is on the regular domain of course and not on the blog.mydomain.com..)
This should do the trick
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com
RewriteRule ^(.*)$ http://mydomain.com/blog/$1 [R=301]

Using .htaccess to redirect traffic to same domain

Well it's kind of hard typing a title to describe what I want to do.
Basically I have a website that I've been asked to develop a mobile site for. This website has various domains (such as .co.za,.com,.za.net) but they all run from the same folder on the server (so I only have one .htaccess file).
I want to be able to redirect the traffic that goes to www.example.co.za to m.example.co.za and traffic that goes to www.example.com to m.example.com.
How would I need to modify this .htaccess file to achieve that.
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|playbook|sagem|sharp|sie-|silk|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte" [NC]
RewriteRule ^$ http://m.example.com/ [L,R=302]
How about:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|playbook|sagem|sharp|sie-|silk|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ http://m.%2/ [L,R=302]

Redirecting Subdirectory to Subdomain with htaccess

I'm relatively new to using .htaccess, and have never done any coding besides what I've read online. I'm using Bluehost, and I'd like to redirect my blog subdirectory to a subdomain. Example: I'd like to redirect www.example.com/blog to blog.example.com.
I already have code in place to always add www. to the beginning of my blog address in the root folder, but I don't know how to accomplish the above redirect by using code in .htaccess. Any help would be appreciated!
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
Edit 2
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
I wanted to add my two cents,
1) to answer the question above, this rewrite should fix it:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^/blog$ http://blog.example.com [R=302,L]
2) but, I think this is not enough by itself, you also need to change DNS, so that blog.example.com is pointed at the right server, and this can be done by a cname similar to this:
blog.example.com CNAME example.com TTL 1080
(not exactly how it will look, but use your DNS webinterface to set this up).
Have you tried this one?
RewriteEngine on
RewriteBase /
RewriteRule ^/blog/(.*)$ http://blog.subdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^check.domain.info$
RewriteCond %{REQUEST_URI} !^/check/
RewriteRule (.*) /check/$1
To redirect subdomain1 and subdomain2 and directory3 to a directory with HTTPS://, I use the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1.example.com [OR]
RewriteCond %{HTTP_HOST} ^subdomain2.example.com [OR]
RewriteCond %{HTTP_HOST} ^example\.com/subdomain3 [NC]
RewriteRule ^(.*)$ https://example.com/subdirectory/$1 [R=301,L]

Resources