Using wildcard subdomain on specific directory - .htaccess

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.

Related

How to redirect multiple domains to woocommerce product page dynamically?

Consider abc.com is my main website using nameservers ns1.testnameserver.com & ns2.testnameserver.com.
I would like to redirect multiple domains as below:
def.com to abc.com/products/def
ghi.com to abc.com/products/ghi
jkl.com to abc.com/products/jkl
mno.com to abc.com/products/mno
I tried the below:
i set the same testnameserver for def.com,ghi.com,jkl.com,mno.com.
tried some combination in RewriteCond and .htaccess but i couldn't solve it.
Anyone guide me to move further.
Thanks all.
Note:
I can't use domain forwarding / host all the domains because it's should be dynamic (like 3500+ domains to 3500+ products)
I'm not sure I understand your problem, but you can redirect domains with:
RewriteEngine on
# not for exemple.com
RewriteCond %{HTTP_HOST} !example\.com$ [NC]
RewriteCond %{HTTP_HOST} (?:^|\.)([^.]+)\.(?:[^.]+)$
RewriteRule ^ http://example.com/products/%1%{REQUEST_URI} [NE,L]
Point all your DNS on the main website, and just change the name of the main domain (example.com) in this .htaccess
You can achieve this using below steps -
You need to setup the zone file for each domain having an A record entry pointing to IP address of abc.com.
On abc.com web server, you can write an script in any language of your choice which will read the host and appropriately does a redirect to wherever you need.
Alternatively you could do this from .htacess too, but since you have a large amount of domains, so I would suggest to go with a script in any language (php, python etc.)
You can do exactly what you are looking for with a RewriteMap directive.
First place your mappings in a file, e.g. /var/lib/domains.txt containing
def.com http://example.net/products/def
ghi.com http://example.net/products/ghi
jkl.com http://example.net/products/jkl
mno.com http://example.net/products/mno
Then you set up the following rules in a server or virtual host .conf -file (you cannot do this in a .htaccess file):
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteMap custdomains txt:/var/lib/domains.txt
# rewrite matching domain to desired URL
RewriteCond ${lowercase:%{HTTP_HOST}} ^(?:www\.)?(.+)
RewriteCond ${custdomains:%1} ^(.+)$
RewriteRule ^/(.*)$ %1 [R=301,L,NE]
# unmatched domains are passed unchanged
RewriteRule . - [R,NE,L]
The RewriteCond -rules will capture the HTTP_HOST header containing the hostname of the target domain the user is browsing to. If that hostname matches a mapping defined in your textfile, the browser will be redirected to the matching URL.
With this setup you can point all your thousands of domains to the same server, which then performs suitable redirects based on the mappings in an external file (which you can update without having to restart apache). For better performance, you should really use an external DBM Hash File instead (see the documentation for RewriteMap for details).

.htaccess, DNS & linkstructure

what I'm trying to achieve is forwarding a domain to a subdirectory of my web server.
I need to have several 3rd party domains pointing to the same content whit preserving the link structure.
So no matter if I type in example.com/page1 or example.nl/page1 or example.eu/example1 there should be always the same content.
Any ideas how to achieve that?
If the goal is to server different content under the different domains, assuming you have a directory structure where each domain is a directory under DocumentRoot, you could use:
RewriteEngine On
RewriteCond %{HTTP_HOST} (\.com|\.eu|\.nl)$ [NC]
RewriteRule .* %{HTTP_HOST}/$1 [L,PT,NS]
Otherwise, just pointing DocumentRoot for all domains at the same DocumentRoot will suffice, albeit with SEO risks.

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 subfolder depending on domain navigated to

I am setting up two wordpress sites on one shared hosting plan for a client. 123-reg says on the decription that many sites can be hosted on the same server so I thought this would be a simple click of a button thing but clearly not.
I have two sites sitting in 2 separate sub directories; FolkstockFestival and FolkstockArtsFoundation. I also have two domains www.folkstockfestival.com and folkstockartsfoundation.com. I need each domain to go to its corresponding sub directory.
I thought you'd be able to do this with the DNS settings but I haven't found a way to do that so I think .htaccess is the way forward. With mod_rewrites, is there a way to direct the user to the right directory depending on which domain is requested? The .htaccess file would be in the root directory.
I do not want the sub-directory to show on the domain so www.folkstockfestival.com shows as is but directs to the correct site.
Many thanks
Use DNS to make both of your domains pointing on your server, and use this htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} www\.folkstockartsfoundation\.com [NC]
RewriteRule ^(.*)$ /FolkstockArtsFoundation/$1 [L]
RewriteCond %{HTTP_HOST} www\.folkstockfestival\.com [NC]
RewriteRule ^(.*)$ /FolkstockFestival/$1 [L]

How to setup .htaccess to rewrite to a different folder

I'm moving my site to a new host but I need to have my current server continue to handle requests (not all files can be moved to the new server).
So I added a parked domain to my old server (old.mydomain.com) and I want all requests to it to be written to the files from the old site.
My old site (mydomain.com) was hosted internally in a folder (/public_html/mydomain/) and I want all requests to old.mydomain.com to be rewritten to the same folder.
So if mydomain.com/blog was internally at /public_html/mydomain/blog, I now want old.mydomain.com/blog also to reach /public_html/mydomain/blog.
Here is the .htaccess that I'm trying to use:
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com/*
RewriteRule ^(.*)$ mydomain/$1 [NC,L]
But for some reason as soon as I add the $1 in the rewrite rule I get an internal error.
Any ideas?
Configure this as a separate vhost called old.mydomain.com and ensure it comes before *.mydomain.com in your vhost definitions (i.e. higher in vhosts.conf). Give old.mydomain.com the same DocumentRoot as your previous domain had.
.htaccess is the most processor intensive way to serve a webpage, and should only be used there are no other options available.
Please try to fix your .htaccess config as follows:
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com$
RewriteRule ^(.*)$ /public_html/mydomain/$1 [NC,L]

Resources