Complex .htaccess redirect advice - .htaccess

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]

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

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 to redirect 2 different domain names to seperate "landing pages"

Upon request I have reformulated my question for clarity so others might benefit better from the answers.
I have 3 domains for the company I work for:
bizwizprint.com (the main website that is hosted on a shared server)
bizwizsigns.com (secondary domain with no hosting attached)
boatwiz.com (tertiary domain with no hosting attached)
The goal is to get my second and third domains to redirect to the first domain onto their own respective landing pages.
First Step: At the domain registrar, change the DNS "A Records" of the second and third domains to resolve to the same IP address that the main website is hosted on.
Second Step: Create a "Site Alias" on the main website server for the second and third domains, they will point to the root directory where the main website files reside.
Third Step: Create or edit an .htaccess file that will redirect the requests for the second and third domains and point them to the landing pages that I have created for them.
The question: What rules do I add to htaccess?
Essentially, I would like to have a user type in "boatwiz.com" in the address bar and the browser will literally GO TO "bizwizprint.com/boatwiz.html".
Please note: I do not want any rewrite rules that will change the actual URL to boatwiz.
The reason for this is that it is a temporary thing. Eventually there will be an actual "boatwiz" website and "bizwizsigns" website and they will most likely be very different in structure. I don't want it to appear that I have three domains with all the same content, or have people make any bookmarks that I will need to redirect yet again in the future.
"How do I redirect an external domain (boatwiz.com) to land in a specific page of a new domain (bizwizprint.com/boatwiz.html) without any rewriting?"
So you probably mean that you want an "internal redirect", not the "external redirect", right? I.e. you want e.g. the bizwizsigns.com to stay displayed in the browser Location bar, but show the contents
of bizwizprint.com/signs, right?
1. htaccess only - impossible
Well, using only .htaccess, this is impossible, because the different domain will force the external redirect. Citing the docs:
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and
hostname are stripped out and the resulting path is treated as a
URL-path. Otherwise, an external redirect is performed for the given
URL. To force an external redirect back to the current host, see the
[R] flag below.
2. Iframe trick
What you could do is to use iframe. Put this code at bizwizsigns.com/index.html:
<iframe src="http://bizwizprint.com/signs" width="100%" height="100%"
style="border: 0 none;" frameborder="0">
But there are many downsides of this solution:
the URL will not change in the browser's location bar, even if user clicks within the iframe
browser bookmarks and history will not work as expected.
3. Clever hosting setup (domain aliases into the same dir)
Are you in a hosting environment, or do you have your own server? Sometimes the hosting allows you to make aliases of several domains that are handled by the same local directory tree. In that case, you won - you can write .htaccess so that it handles the requests as internal redirects:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ /signs [L]
RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ /boats [L]
which will (internally) redirect bizwizsigns.com to /signs (= your content of bizwizprint.com/signs, because you have one hosting server directory for all 3 domains). But if you e.g. want all queries like bizwizsigns.com/<foo> to be redirected to bizwizprint.com/signs/<foo>, you have to be more careful - see the added condition on REQUEST_URI to prevent endless loop:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/signs/ [NC]
RewriteRule ^.*$ /signs/$1 [L]
Assuming that you have all 3 domains pointing to the same document root, you just need this in its htaccess file:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/signs [L,R=301]
RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/boats [L,R=301]
RewriteCond %{HTTP_HOST} (bizwizsigns|boatwiz)\.com$ [NC]
RewriteRule ^(.+)$ http://bizwizprint.com/$1 [L,R=301]
So if it's just http://bizwizsigns.com/ or http://boatwiz.com/, then you get redirected to http://bizwizprint.com/signs or http://bizwizprint.com/boats. But if you have anything after the last /, like http://bizwizsigns.com/foo/bar.html then you'll get redirected to http://bizwizprint.com/foo/bar.html.

Redirect But Not Show The URL redirec :: htaccess?

I need to redirect some users, possibly to a subdomain, but I do not want them to know that they are in a subdomain. They should think they are in the main domain.
I believe this can be done with htaccess, but it is gibberish to me.
Can someone please throw me some bones?
When serving content from a different domain without redirecting the browser (thus changing the URL in the address bar) one of two things needs to happen. Either there is a file-path resolution to the other domain, or a reverse proxy must be set up (and it's pretty easy to do if mod_proxy is loaded).
It looks like you have your subdomain inside the document root of your main domain, which means this option will be viable. So if you want to it so when someone puts this URL in their address bar, http://domain.com/page, they get served the content in http://sub.domain.com/another-page, you'd simply add these rules to the top of the htaccess file in the document root of your main domain (public_html):
RewriteEngine On
RewriteRule ^page(.*)$ /subdomain_folder/another-page$1 [L]
Otherwise, the second option is to use mod_proxy:
RewriteEngine On
RewriteRule ^page(.*)$ http://sub.domain.com/another-page$1 [L,P]
EDIT:
I want to redirect some users, depending on their ip (geographic location) to another subdomain. And yes public_html is the root
You can check against the IP via the %{REMOTE_ADDR} variable:
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.89$
RewriteCond !^/subdomain_folder
RewriteRule ^(.*)$ /subdomain_folder/$1 [L]

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources