Redirect and rewrite based on domain name - redirect

What I need to get working:
I need to redirect based on the domain name from the request, and once the redirection has occurred I need to rewrite the url so that users only see that domain name in the url.
Specifics
My site currently has the domain name example.com and I have a second domain name different.com that I need to redirect traffic to a specific url. This url will switch to a different store view: example.com?___store=differentstore
The switching is working fine, but I need to figure out how to setup a rule in nginx that will watch for the different.com and then redirect to the above url but from that point rewrite the url to different.com so that those users never see example.com.
The only time I need the ?___store=differentstore is on the initial entry with different.com as the host. From that point on users won't be able to see the default view (for example.com) unless they browse to the example.com domain
What I've tried:
This is what I have tried so far and it isn't working:
server {
listen 80;
server_name www.different.com different.com;
return 301 http://www.example.com?___store=different;
if ($http_host != "www.example.com" && $http_host != "example.com") {
rewrite ^ http://different.com$request_uri;
}
Details
LEMP stack
Magento 1.7

Related

Cloudflare forward URL page rule from subdomain to root domain for all routes not working

I created a Cloudflare page rule that would forward https://www.example.com/ to https://example.com/ using these expressions from this documentation: https://support.cloudflare.com/hc/en-us/articles/200172286-Configuring-URL-forwarding-or-redirects-with-Cloudflare-Page-Rules. The forwarding does work for the / route but if the user were to type the URL https://www.example.com/first it would redirect the client to https://example.com/. It should be directing the client to https://example.com/first. These are the expressions I used from the documentation I mentioned but they don't seem to be working:
I believe that you need $2 i.e. redirect to https://example.com/$2. Your current setup should be redirecting https://www.example.com to https://example.com/www
The pattern matching in cloudflare will match each asterisk in the pattern. If the pattern is https://*.example.com/* and you request https://www.example.com/somepath then $1 will equal www and $2 will equal somepath.

Cloudflare redirect domain

I'm trying to redirect domainA.com to domainB.com. Both domains are managed with Cloudflare. I've tried using Page Rules for domainA to redirect all requests to domainB.com. However, I've also read that domainA needs to have DNS records of some sort through Cloudflare but I'm not entirely sure how to configure these? This redirect is the only thing I'm using domainA for, there's no content hosted on it at all.
A combination of a mock CNAME record and a Page Rule will make it.
Go to your source domain's DNS settings page.
Create a CNAME record for the # (root domain). Point it to some subdomain, it doesn't have to exist as the page rule will kick in before the DNS record resolves.
Go to your source domain's Page Rules page.
Create a new page rule redirecting all requests to your domain as necessary (choose 301/302 - whichever makes sense in your case).
Give it some time and confirm https://source-domain.com redirects to https://target-domain.com.
Repeat steps 1 - 5 for the www subdomain if you want www.source-domain.com to redirect too (which is often the case).
Set a forwading page rule 301 Redirect
from
http://domainA.com/*
to http://domainB.com/$1
In 6 steps you have new domain.
In your control panel, select the domain
Click on page rules
In 'url match' set example: (domainA.com/*)
Setting is 'forwarding url' with 302 (test) or 302 (production)
And finnaly set the new url ex: (http://domainB.com)
Save and deploy
for full documentation see this:
https://support.cloudflare.com/hc/en-us/articles/200172286-Configuring-URL-forwarding-or-redirects-with-Cloudflare-Page-Rules

nginx wildcard redirection configuration

I have a web application that is running on myhost.com
When a user clicks on any of the buttons on the page I want to redirect them to the same page hosted on a different server. So for example I want myhost.com/x/happy to redirect to uathost.com/x/happy
I am trying to use wild card so that every /x/* page is redirected appropriately.
I tried to use the below configuration but I still get a 404 from myhost.com when clicking on any of the buttons since myhost.com is not configured with those pages, thus my need for a redirect.
location ^~/r/ {
proxy_pass https://uathost.com/$1;
}
Well are you redirecting or proxying? The code you are showing is for a proxy to uathost.com not redirect.
For a redirect all should have to do is:
location /x {
return 301 https://uathost.com$request_uri;
}

The right way to do a 301 NGINX redirect without losing backlinks

I faced an issue with the Google Dec. 2016 update, and lost 99% of my traffic (200K/day to 1K/day). As a result, I've created a new website in the same niche. Now, my goal is to transfer the DA (Domain Authority) to my new domain from the old one. However, while trying to accomplish a 301 redirect such a way
rewrite ^ $scheme://new-domain.com$request_uri permanent;
I faced another issue.
I have a lot of quality backlinks from WSJ, NYT, Wikipedia, etc., which direct to some particular pages on my old domain. If I use such a redirect, these quality backlinks now point out to the empty pages (404).
I have thousands of pages on my old domain, and I can't create so many pages one more time on my new domain.
Here are my primary questions:
1) How can I accomplish a 301 redirect without losing my old quality backlinks?
2) Is there a method to redirect all the old backlinks to my old domain to the main page of my new domain? In other words, I want to redirect all the domain authority from all the old backlinks to the main/index page of my new domain.
I use NGINX.
P.s.:I wasn't able to discover the exact answer to my question on Stack Overflow.
You have a few options, depending on a few things.
Is the old domain hosted on the same server as the new one, with all the old files accessible? If so are you interested in serving those pages still, either from the new domain or the old one, or do you just want all traffic to the old domain to land at the new one?
If its the last option it's easy, you just create two server blocks, one for each domain.
First up the old one, we'll get rid of your rewrite directive as return is more suitable and set up the first block to catch everything to old domain and send it to new domain:
server {
listen 80 default_server;
server_name old-domain.com;
return 301 https://new-domain.com/ # Everything to homepage, or:
return 301 https://new-domain.com$request_uri; # This will include the full request url
}
server {
listen 443 ssl http2;
server_name www.new-domain.com;
...
}
If you still want to serve content from the old domain you can do that too, depends on your aim.

Redirect domain to another domain in Meteor

I have two domain names, one which I've been using for a long time (blainehansenpianostudio.com), and another simplified one I'm now using (blainehansenpiano.com). I'm hosting with Digital Ocean, and my DNS is set up so that both domains point to my droplet, and both show up correctly with my site.
However, I want blainehansenpianostudio.com (the longer, more annoying one) to completely redirect to blainehansenpiano.com, but currently it just acts as an alias, showing the same site but also keeping its annoyingly long url.
The only things I've been able to dig up about http redirection are this meteorpedia article and this stackoverflow question. Neither of these seem to be taking into account the existence of a secondary domain, and are using the in-app routing system. That doesn't seem to be the correct solution, since the redirect should be happening even before the bundle is sent right? I don't want a bunch of extraneous redirects happening within my app.
Also, the SO question has this line:
The easiest way to achieve this is to put the redirect in middleware:
which doesn't mean anything to me. Where is "middleware"?
How do I go about accomplishing this? I feel like I need to change something in the server setup, but I deployed with Meteor Up, which doesn't mention anything about redirects, and am not sure where the server config I need even is.
Thanks in advance!
Update
I tried putting the following code in my lib/router.js file, just to test things out and see if the router is the "middleware" in question:
WebApp.connectHandlers
.use(function(req, res, next) {
console.log(req);
console.log(res);
console.log(next);
next();
});
but it absolutely wasn't. It just broke the router.
Possible (but messy) Solution
Putting this client-side redirect code into main.js:
if (window.location.href.indexOf("blainehansenpianostudio") > -1) {
window.location = "http://blainehansenpiano.com";
}
works, but it seems to lag for a moment after the redirect is made. It seems to me that this is triggering two entirely separate server requests, which is not at all ideal.
For a meteor app, you can use a the javascript to do a conditional redirection. An example below:
<script type="text/javascript">
<!--
function Redirect() {
if(window.location.href.indexOf("blainehansenpianostudio") > -1){
window.location="http://blainehansenpiano.com";
}
}
//-->
</script>
Are you using any other reverse proxy in front, like Apache, or Ngnix? If so, there can be better ways to achieve this.
If you install Ngnix (which is the cleaner way) for server side redirects, then you may include a new server block for performing the redirect. An example is below:
server {
#implemented by default, change if you need different ip or port
#listen *:80 | *:8000;
server_name blainehansenpianostudio.com;
return 301 $scheme://blainehansenpiano.com$request_uri;
}
In your case, you might choose to hard code the $scheme to http. $request_uri part ensures that the original request URI is included in the redirect response.
To set up nginx, I would also suggest the use of link provided by #Steffo
I use nginx in front of the meteor app (also for SSL termination - I saw that you have a sign-in button on the site, so you maybe want SSL) and it runs also on DO. Use a single nginx instance to accept requests for both domains and reverse-proxy them to a single meteor instance. I wouldn't use redirects inside the meteor app as this could easily interfere with SSL setup
server {
listen 443 ssl;
server_name www.blainehansenpiano.com;
ssl_certificate /etc/ssl/blainehansenpiano.crt
(... SSL stuff )
location / {
proxy_pass http://meteor_localhost-OR-remotehost:3000;
(... some web socket setting ...)
}
}
and also the longer name pointing to the same meteor instance
server {
listen 443 ssl;
server_name www.blainehansenpianostudio.com;
ssl_certificate /etc/ssl/blainehansenpianostudio.crt
(... SSL stuff )
location / {
proxy_pass http://meteor_localhost-OR-remotehost:3000;
(... some web socket setting ...)
}
}