Redirecting Subdirectory to Subdomain with htaccess - .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]

Related

how to redirect subdomain to actual website

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]

Domain redirection with folder separation. htaccess

After redirecting my client domain to my server i created rewrite rule in root folder .htaccess file to point domain to a subfolder1 (joomla website):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/subfolder/
RewriteRule ^(.*)$ /subfolder1/$1 [L]
Everything worked fine but today i found out that i can access any other subfolder in my server by entering for example: myclientdomain.com/subfolder2 and what's worse google can index that and show it in search results.
If there is any way to redirect a domain in a way that I won't be able to access any other folder on my server?
I would really appreciate help as I searched throughout google for answer, my server tech support said that they don't really support these kind of problems (they only gave me a piece of code from above) and I don't really know anything about .htaccess rules and how it works.
Try this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder\/?(.*)?$
RewriteRule ^(.*)$ /subfolder1/$1 [L]
Change above rule to:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
# if current URI is not starting with /subfolder/ then route to /subfolder/
RewriteRule ^((?!subfolder1/).*)$ subfolder1/$1 [L,NC]

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]

Htaccess redirect without url rewrite

I have a little problem in hands.
I am setting up a domain that as 3 languages, example.com for principal domain, en.(...) for english and de.(...) for germany.
Usually I only redirect the httpdocs from the subdomains to the main with ln -S because all languages executes the same code, the difference is matched from php and mysql.
My new host don't provide any ssh connection so I have to use a different approach on this.
It was told to me that it can be done by .htaccess but I already tried a lot of things but only can redirect, changing the url and that's not possible, it have to keep the same, the contents yes, will be from another domain. Can someone help?
This code should looks like the one you're searching for :
www.domain.en .htaccess
RewriteBase /
RewriteRule ^(.*)$ http://www.domain.com/en/$1 [L,R=301]
www.domain.de .htaccess
RewriteBase /
RewriteRule ^(.*)$ http://www.domain.com/de/$1 [L,R=301]
You'll propably adapt the http://www.domain.com/lang/$1 part to your needs.
EDIT
Following your comment, this is a code for subdomains redirections :
domain.com .htaccess
RewriteCond %{HTTP_HOST} ^en\.domain\.com [NC]
RewriteRule (.*) http://domain.com/en/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^de\.domain\.com [NC]
RewriteRule (.*) http://domain.com/de/$1 [QSA,L]

Resources