.htaccess, DNS & linkstructure - .htaccess

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.

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).

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.

create subdomain to forward to sub-folder on a different domain

I've read some stuff on this but cannot get it clear (or to work):
I have a domain (call it domain1.co.uk) registered with namesco, but pointing to web hosting elsewhere - the name servers are set to those of the hosting company.
With the hosting company, I have a number of different websites in folders under public_html.
I have another domain (call it domain2.co.uk) also registered by namesco, that redirects to one of the websites mentioned above (ie www.domain2.co.uk forwards to www.domain1.co.uk/thisparticularwebsite/home.php).
What I want to do is get a subdomain on domain2 (sub.domain2.co.uk) to reference a specific php script in thisparticularwebsite (ie www.domain1.co.uk/thisparticularwebsite/sub.php).
namesco tell me I can do this by creating an A record or a CNAME record, but it can only reference the top level domain (public_html at domain1), not a sub-directory or script.
What I think need to do therefore is use .htaccess as follows in the root directory of domain1.co.uk
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain2\.co\.uk [NC]
RewriteRule ^.*$ http://www.domain1.co.uk/thisparticularwebsite/sub.php [R-301]
or possibly
....
RewriteRule ^.*$ /thisparticularwebsite/sub.php [R-301]
and then create a CNAME record on domain2 for sub (ie sub >> domain1.co.uk).
Is that right?(it doesn't seem to work, though it is unclear what is going on as DNS updates may not have propagated fully yet).
One thing I am not sure if is what should actually turn up in HTTP_HOST - is it the string as typed in by the originating client, or does it get modified along the way by the CNAME records?
.htaccess code should be
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain2\.co\.uk$ [NC]
RewriteRule ^.*$ http://www.domain1.co.uk/thisparticularwebsite/sub.php [L,R=301]

htaccess rewrite secondary domain to subdomain

I have a main domain "www.main.com" with subdomain "subdomain.main.com"
the subdomain is located in the folder /htdocs/subdomain/ and the main domain is a symbolic link to the /htdocs/www/ (which also serves www.main.com)
Now I have registered a new domain name, "newname.com" which I have connected to my hosting account. Now I am trying to determine the contents of /htdocs/.htaccess for rewriting incoming requests to subdomain.main.com.
To be clear, I do not want to redirect. I want www.newname.com/anything to be rewritten to subdomain.main.com/anything, while the user keeps on navigating newname.com.
I fear there is a catch with circular rewrites?
What should my .htaccess look like?
My document root is /htdocs and the subdomain and the new domain are on the same ip address.
First of all, make sure your DNS is properly setup i.e. ping www.newname.com goes to the same server (= same IP) than ping subdomain.main.com.
Then your vhost should look like:
<VirtualHost *>
ServerAdmin webmaster#papdevis.fr
DocumentRoot "/home/subdomain"
ServerName subdomain.main.com
ServerAlias www.newname.com
</VirtualHost>
Then everything should work transparently. It's all about vhosts. I don't know with htaccess, sorry.
It was in fact easy, i created a symbolic link to my new domain (primary.com/secondary) which links to the folder which also serves secondary.primary.com and I rewrite all queries to www.secondary.com to that symbolic link. Totally untransparent, just how I wanted it. It was just a bit fighting with drupal before it let me take control of a tiny part of my main domain :)
The main .htaccess is
RewriteCond %{HTTP_HOST} ^www.secondary.com [NC]
RewriteCond $1 !^secondary/
RewriteRule ^(.*)$ /secondary/$1
and i redirect all secondary.com to www.secondary.com in my rewrite base with this .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} secondary.com$ [NC]
RewriteRule ^(.*)$ http://www.secondary.com/$1 [R=301,L]

.htaccess redirecting with two domains

I currently have two registered domains that redirect to the same hosting package. So, as an example I have the following two domains:
www.domain1.com (Attached to the hosting)
www.domain2.com (just redirects to www.domain1.com)
Both go to the same index page.
However, I would like to change this so that surfing to www.domain1.com would make you stay on the default index file and that surfing to www.domain2.com (or without the www) would make you go to a seperate folder (example www.domain1.com/domain2)
I'm guessing manipulating the .htaccess file is the way to go?
RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain2.com
RewriteRule ^index.html index2.html
Not sure what your default index file is, but you can edit the Rule to match.

Resources