redirect only www and root domain - .htaccess

I have an old domain that I have redirected to a new domain, using
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
In reviewing the incoming links to my old site, I found out there are hundreds of spammy links pointing to random sub-domains on my old site. So, my backlink profile has links to things like adfdfdf.oldsite.com , etc.
I don't want these old spammy links forwarded, as they now point to www.newsite.html, thus giving me spammy links to my new site home page.
What I would like is for the oldsite to only redirect the root domain and the www subdomain, but no other subdomains.
I tried a couple of different configurations but couldn't get one to work properly. Any ideas?

Add this condition:
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite.com$ [NC]
right bfore your redirect rule.

Related

301 redirect for OpenCart (old url to new)

I am looking for a 301 redirect. I have a OC 1.5.1.3 (abc.com) and have a new store now on OC 2.0.3.1 (xyz.com ). I have migrated my data from abc.com to xyz.com and since all the product / pages urls are the same except the domain change, i am looking for a simple code which i can add to htaccess to make this happen.
Since my old website urls are listed on Google, i would like to redirect the traffic coming to a particular url to the same page on the new website. It would be a permanent redirect.
I paid a freelancer to get this done.. was in a hurry. Anyways, i have posted the answer below for the benefit of all.
Upload this to .htaccess on your old domain abc.com, replace example.com with your new domain name.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !example.com/$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Complex .htaccess redirect advice

I would like to redirect all users who visit pages on my old domain to the same page on my new domain (only the domain name is changing).
If my old domain is www.OLDDOMAIN.com and my new domain is www.NEWDOMAIN.com then I want to be able to redirect visitors who visit www.OLDDOMAIN.com/c/123/Page-Name to www.NEWDOMAIN.com/c/123/Page-Name (note that these are SEO friendly URLs and not simply sub directories or filenames)
However, I do not want this rule to apply to visitors from a certain IP addresses (lets say 2 IP address 123.123.123.123 and 345.345.345.345 - I appreciate these aren't valid).
I would also like to ensure that all subdomains e.g. files.OLDDOMAIN.com are NOT redirected to files.NEWDOMAIN.com
AND I would like the ability to add exclusions to this rule. E.g. I want to continue to be able to access www.OLDDOMAIN.com/index.php or a subfolder e.g. www.OLDDOMAIN.COM/admin/
The reason for need to do this is the website currently on www.OLDDOMAIN.com is being moved to www.NEWDOMAIN.com. and www.OLDDOMAIN.com is being repurposed for another website. The reason I need the redirects is to retain search engine rankings, at least until the new site is established. The solution therefore needs to be SEO friendly.
Apologies in advance for not supplying any code but I'm not entirely sure where to start with this one...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# exclude these IPs
RewriteCond %{REMOTE_ADDR} !^(123\.123\.123\.123|345\.345\.345\.345)$
# exclude these URIs
RewriteCond %{REQUEST_URI} !^/(/index\.php|admin/) [NC]
# exclude sub-domains
RewriteCond %{HTTP_HOST} ^(www\.)?OLDDOMAIN\.com$ [NC]
# redirect to new host
RewriteRule ^ http://www.NEWDOMAIN.com%{REQUEST_URI} [R=302,L,NE]

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 rewrite all pages to same page on another domain

So I've been looking but so far can;t find something that I'm looking for exactly. Looked at this article How can I forward ALL pages to the same exact page on a different domain using .htaccess? but it seems to only redirect my home page.
Currently, I'm using WordPress and I need to be able to forward all pages to a new domain. For example, domain1.com/about-us needs to go to domain2.com/about-us. But I have about 50 pages this needs to work on. I would like to see if there is a 1-5 line code to use for this to work.
Thanks!
Try putting this (above any wordpress rules) in the htaccess file in domain1.com's document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1.com [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301]
If your 2 domains are one different webservers, or don't share a common document root, you can just use mod_alias, adding this to the htaccess file in domain1.com's document root:
Redirect 301 / http://domain2.com/

.htaccess old domain to new domain 301 redirect, but only the domain, not it's pages

Let's say I have a domain: A.com, and if someone goes to A.com I want to redirect to B.com.
But if they go to any page on A.com, e.g. A.com/wp-admin/index.php, there will be NO redirect. In short, I just want a simple top level old name to new domain name 301 redirect. (Any specific pages on A.com will be redirected to specific pages on B.com one by one).
Here you go:
RewriteEngine On
RewriteCond %{HTTP_HOST} =domain-one.com
RewriteRule ^$ http://domain-two.com/ [R=301,L]
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
It will redirect hits to domain root only, e.g. http://domain-one.com/ and no other pages.
I have replaced A.com by domain-one.com and B.com by domain-two.com -- it is more realistic/understandable than A & B.

Resources