HTACCESS Forward traffic from temporary domain to new domain - .htaccess

I have two working domains on the same server.
How can I forward all traffic to all files on the old temporary domain to the new correct domain?
Seems like this might be an SEO issue?
But, since the temporary testing domain isn't registered, would it even be crawled?
Is this even a problem?
I'm currently using:
RewriteEngine On
# Force to WWW
RewriteCond %{HTTP_HOST} ^thecorrectdomain\.com
RewriteRule (.*) http://www.thecorrectdomain.com/$1 [R=301,L]
#
The incorrect domain is the setup that we used for testing, before we pointed the domain name to the new server.
The second is the correct domain.
The incorrect testing domain looks like:
http://1234.hostingcompany.com/~username/index.php
The intended correct domain looks like:
http://thecorrectdomain.com/index.php
Or is this something that the hosting company has to do at their level?

Assuming hostingcompany supports .htaccess. You create a .htaccess in http://1234.hostingcompany.com/~username/ which contains something like:
RewriteEngine On
#might not be necessary
RewriteBase /~username
RewriteRule .* http://thecorrectdomain.com/$0 [R=301]
PS while testing use 302, instead of 301.

Related

Forward all URL requests to another domain (and keep original requested path) - Htaccess or DNS?

I want to buy a new domain, and have it redirect all page requests, to another domain (example.com).
So if someone goes to newexample.com/page-1, he will get a 301/302 redirect to example.com/page-1
If I do this with htaccess, it means I need to buy hosting first, but maybe that's not necessary and I can do with some kind of DNS definitions?
If it would be with htaccess, Should it look something like this?:
###
RewriteCond %{HTTP_HOST} ^\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]
###
I think a proxy server (eg. Nginx, HAProxy) will be a better option for this use case.

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

Redirect from old subdomain/domain to new domain using htaccess

My website was on a subdomain, like this: mysite.primarydomain.com. The primarydomain.com DNS was managed by a third party who owns the primarydomain.com url.
My new website is now on on mysite.com. I had the third party add an a record that points mysite.primarydomain.com to my new website's IP address. How can I then redirect that mysite.primarydomain.com domain to mysite.com? Would that be via htaccess? Or via cpanel configuration?
Thanks!
There's probably a cpanel configuration to do this pretty easily, probably something like this: http://docs.cpanel.net/twiki/bin/view/AllDocumentation/WHMDocs/SetupForwarding
But you can also add an htaccess file to the document root of the mysite.primarydomain.com site, that says:
Redirect 301 / http://mysite1.com/
or if the document root is shared, you'll need:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\primarydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mysite1.com/$1 [L,R=301]
Thanks Jon Lin for your answer. It inspired me to find a very easy method in cpanel...
In the primarydomain.com, I created a subdomain for "mysite.primarydomain.com".
I then went to "Redirects". Since I added the "mysite." subdomain, it gave me the option to create a redirect using that subdomain. So I created a redirect for mysite.primarydomain.com to point to mysite.com.
I then was able to create a few other redirects to help keep some of their old links live, such as "mysite.primarydomain.com/about".
Thanks!
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain.domain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

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]

How can I rewrite all traffic to sub-directory except sub-domains using .htaccess?

So the hosting provider I use doesn't support virtual hosts or anything similar so I'm trying to rewrite my primary domain to a sub-directory. So for example I currently have:
example.com rewritten to example.com/sites/example.com/
This works fine using the following code in .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/sites/example.com/.*$
RewriteRule ^(.*)$ /sites/example.com/$1
The reason for this is that have other domains on the same server that I don't want to be accessible by the primary domain and to keep my server organised.
So I also have example2.com as an add-on domain pointing to /sites/example2.com and that works fine.
However, I also have sub-domains that are pointed at different sub-directories such as projects.example.com pointing to /projects/ but these just get rewritten to the /sites/example.com/ directory.
My question is how do I (and is possible too) resolve this? Thanks in advance!
If I truly understand, you should to add host condition, if user is on primary domain - he will be redirected. Check this, I've modified a code a little bit:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#Here we're capturing host name, if it has only one point - this is not subdomain.
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+\.[a-z]{2,5})$
RewriteCond %{REQUEST_URI} !^/sites/%1/
#Redirecting to the current host name in folder sites
RewriteRule ^(.*)$ /sites/%1/$1 [L]
EDIT:
This was not elegant decision to add regex for domain, it will work only for second level domains, if you're using domains like .co.uk - you have to add them manually in rule like RewriteCond %{HTTP_HOST} ^somesite.co.uk$.

Resources