.htaccess and A record subdomains - .htaccess

I have a wordpress hosting account. I also have a standard web hosting account with the same host. My main website, domain.com is hosted on the Wordpress platform, BUT now I want to add a subdomain for sub.domain.com.
I can't host the script on sub.domain.com as part of the wordpress hosting, so I have been told to create it on my standard web hosting account and then use an A record in the DNS for domain.com to point to the IP of sub.domain.com.
Now, all this works if I visit http://sub.domain.com. However, www.sub.domain.com doesn't work.
What I want to know is, can I edit the .htaccess for domain.com to redirect anyone who visits www.sub.domain.com to http://sub.domain.com?

You can use this:
RewriteCond %{HTTP_HOST} ^www\.sub\.domain\.com [NC]
RewriteRule ^(.*) http://sub.domain.com/$1 [L,R=301]

You can't use .htaccess to redirect traffic from www.sub.domain.com to sub.domain.com, because that traffic won't ever get to your server unless your DNS is set up correctly. In other words, if there's no DNS record for www.sub.domain.com, traffic looking for that address will never hit your server, so what is in your .htaccess would be irrelevant.
Keep in mind that you don't have to have www.sub.domain.com - almost nobody will visit that unless you share links using that domain yourself.
That said, if you really want to do this:
Create a sub-sub domain by creating an A record for www.sub.domain.com
Edit the .htaccess file for that sub-sub domain only, adding this rule:
RewriteRule ^(.*) http://sub.domain.com/$1 [L,R=301]

Related

Redirect subdomain into a folder of another domain

I'm trying to redirect one subdomain (a wordpress.com domain) into a folder of another domain. Example:
blog.domain.com to anotherdomain.com/blog
I know that is possible by using .htacces files, but i dont have a hosting service on my first domain.
Is it possible to do it by using DNS?
I have tried by creating some A and CNAME registrations but I cant find a way to do it: Can I have a ip for a specific folder of my second domain?
Thanks :)
I attempted to do a comment but it's too much. You can do this if you own both domains. If you don't have hosting on the first domain but you do on the other domain, then you can add the main domain as an ServerAlias on the other server.
Either through your control panel or your vhost config do the following.
Add an A record for blog.domain.com in DNS pointing to the IP address of the other domain with the hosting account.
In the configuration of the web server with the hosting add a ServerAlias of blog.domain.com to the anotherdomain.com vhost config.
in the root of the anotherdomain.com put this code inside your .htaccess file.
If you want it to actually redirect then you can do this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/blog/$1 [R=301,L]
If you don't want it to redirect and keep blog.domain.com in the address bar then, you can do this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.*)$ /blog/$1 [L]

How to give a subpage it's own domain name?

I have a Joomla site mysite.com.
I want to have the subpage mysite.com/example act like a separate website using a separate domain name anothersite.com.
Can this be achieved using DNS settings on the domain and htaccess?
Hosting sites at cloudaccess.net but want this solution for one-page sites and landing pages without using a whole other hosting account / joomla instance.
Thanks.
If your hosting provider allows you to have "alias" domains, you can point the new domain at the existing code, and detect the hostname using RewriteCond:
Start by configuring the new domain to display all the same content as the existing domain.
In the Apache configuration (probably .htaccess on shared hosting) add a rule which detects requests for the new domain, and "rewrites" them to instead serve your specific page:
RewriteCond %{HTTP_HOST} landingpage.example.com
RewriteRule ^/$ /url/for/my/landing/page
Add an additional rule afterwards so that other URLs on that domain redirect the browser back to the main site
RewriteCond %{HTTP_HOST} landingpage.example.com
RewriteRule ^/([^/].*)$ http://realsite.example.com/$1 [R=permanent,L]
A variation of this would be to map everything on the second domain to a directory (or, rather, a URL prefix) in the first domain, by combining those two rules:
RewriteCond %{HTTP_HOST} subsite.example.com
RewriteRule ^/(.*)$ /url/for/my/subsite/$1 [L]
Note: If Joomla thinks it knows the "correct" URL for the page, it may try to redirect away from your new domain. If so, that is the topic for a new question.
If, on the other hand, you want to use Apache configuration to force users accessing http://realsite.example.com/url/for/my/landing/page to be redirected to http://landingpage.example.com/, you could add something like this:
RewriteRule ^/url/for/my/landing/page$ http://landingpage.example.com/ [L,R=permanent]
Note: Depending on some environment settings I don't fully understand, you may need to change ^/ to just ^ in the patterns above.

.htaccess redirect to oldsite subdomain

I used to have domain say example.com now I hosted new site on this domain, but I could not properly took all the site contents to new site. So, I created subdomain oldsite.example.com and pointed it to my server ip for previous example.com.
Everything is fine here I see my oldsite when I type oldsite.example.com. But rest of the url still refers to example.com/xxx.
All I want to redirect using .htaccess any request coming for example.com/xxx on my previous hosting server to point to oldsite.example.com/xxx.
Note : It's wordpress site.
Putting this into the htaccess in the root folder for the example.com site on your old host, should achieve what you are wanting
RewriteEngine on
RewriteRule ^(.*)$ http://oldsite.example.com/$1 [R=301,L]

Using wildcard subdomain on specific directory

I am setting up an existing wordpress install to use multisite. I have successfully set up the network and since it was an existing install it uses subdomains (which I wanted anyways).
I have created a new site/subdomain on my network so it would look something like this example.domain.com
The issue I am running into is getting wildcard subdomains setup on my hosting provider BlueHost.
I am hosting multiple domains so when I setup a wildcard it always points to the root or original ip address.
I need it to point to a specific directory of the root so it uses the correct wordpress install. root/specific-directory
Is there a way to achieve this? I am not too familiar with a and cname issues. If there is a way to achieve it by writing in the htaccess that would be okay too.
I am hosting multiple domains so when I setup a wildcard it always points to the root
I'm assuming you meant that you get the same content for example.domain.com as if you were visiting www.domain.com. This can surely be handled through htaccess rewrite rules.
If a sub-domain named subsite was to be served out of ${DOCUMENT_ROOT}/subsite directory, you would add the following rules to your .htaccess placed at your web root / directory:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subsite\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subsite [NC]
RewriteRule ^(.*)$ /subsite/$1 [L]
If there are several mutisite subdomains that need redirection, use the following dynamic rules instead:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*?::\1
RewriteRule ^(.*)$ /%1/$1 [L]
You would also need to add ServerAlias *.domain.com to your <VirtualHost> configuration in Apache's httpd.conf file.
It seems BlueHost doesn't take up any requests to modify httpd.conf directly for you. But, they do provide a way to setup a wildcard subdomain from their Control Panel.
Take a look at How to Set Up a WordPress MultiSite in Bluehost. The last section outlines the process with screen shots. You also need to enable theme per site for your subdomains.
1- You can add your subdomain as an addon domain and it can have a separate subdirectory as you want.
2- Another way to achieve this is using another host for subdomians. On the second host you set up your subdomains and point your subdomains from your main host to subdomains on the secondary host.

Webfaction subdomain forwarding

I have shared hosting on webfaction and I want to have www.mydomain.com forward to mydomain.com, in the same way that www.stackoverflow.com redirects to stackoverflow.com. In the webfaction control panel I set up a CNAME record linking the www.mydomain.com subdomain to mydomain.com, but this doesn't seem to be working. Maybe what's messing it up is that mydomain.com is a virtual host and doesn't have an A record to an IP address?
Anyway, can someone help me figure out the right way to do this, either in the webfaction control panel or directly in the httpd.conf file? Thanks in advance.
The solution I use is to create a Static/CGI/PHP application (which I call redirect), and place an .htaccess file at its root. I point all the domains and subdomains that need redirecting to this application, and I then populate the .htaccess file with all the necessary redirection directives for my server, like so:
RewriteEngine on
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} www.example1.com
RewriteRule ^(.*) http://example1.com/$1 [R=301,L]
It works well, and because it is instantaneous, it's easy to debug.

Resources