How to Redirect from WWW to Non-WWW on NGINX. Works for Http to Https too!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody welcome back to the deaf marketer Channel today we've got a quick tip for you guys out there if you're running nginx what I want to show you guys today is how you can redirect from a wwwo main to a non W domain this is something that I think most people end up having to setup it's actually really easy to do with nginx I'll show you guys how to do it in today's video this will also work obviously vice-versa so if for some reason you need to redirect from a non www you can do that as well and then I prefer the non wwas but whatever if you want to go the other way the same technique will work also this will work for non HTTP traffic redirecting non-secure traffic to secure traffic if you have that set up so this is gonna work multiple ways so this same technique is really really handy to have let me show you guys how it works [Music] hey welcome back so over here on my left hand side what you can see is I've got a terminal window we are gonna use this terminal window to SSH into our server in just a couple seconds here above me you can see that we've got a we've got a website here so this is a website that I just put up for a client today go blockades a pest control company so blockades the name of the company so anyway go blockade if you look at it right now if we go to www.ewtn.com I goal is by the end of this video we're gonna be able to go to dot 2ww and get redirected to the non WW version also I want to redirect back to the secure website because there is as you can see we already have secured an SSL encryption setup and everything like that so that's the goal of today's video let's SSH into the server connect to it change these settings and put it to work so what we're gonna do here is I'm gonna use the terminal to SSH into the server so we're going to do SSH give it your username and your domain name and log right in so you can see I'm running in Boone 216 it doesn't really matter what you're running though I mean assuming you're running some sort of Linux but it otherwise it wouldn't really matter because it's all this is all about nginx ok so I'm logged in to the server now what we're going to be doing if I just to run a quick LS command I'm you can see there's a folder here for my website so this go Blockade website is contained inside of this folder and nginx points to this folder to serve this website ok so just letting you guys know that's how this is set up now what we need to do is we need to get into the configuration file for this website ok and or whatever website you're working with that you want to redirect this at so for most people this is going to be inside of your etc' directory nginx and inside of this directory if we do another LS command you can see I'll have several files it's possible that it'll either be inside of your engine X config file Kampf or for the way I've got mine set up as my configurations are inside of the sites available and then I have a different config for every site that on this website would on this server which is just one but that's the way that I was always taught to set it up so that's the way I've got it done no sites yeah I don't know what I was doing okay sites available let's get into that folder all right and then now that we're inside of the sites available it's LS real quick you can see there's a catch-all and there's go blockade com so go block is obviously the site that we're gonna work with now we're going to do Nano which is the text editor that's kind of built into Linux will use Nano it's the easiest to use obviously there's other programs that are more powerful like vim but they take up they have a little bit more of a learning curve so what we're gonna do is we're going to use sudo - to open this as a super user because otherwise we're not gonna be able to save it when we're done so well you might be able to I guess it depends on your your configuration but unless here we're logging directly in is like the admin group then you'll probably have to use sudo so we'll do sudo and then Nano to open up a Nano and then we'll open up this file for the configuration file for our website now it does look like I need to type in my password okay so you're not going to see anything when you type in the password but then you're gonna click enter all right so now that you're inside of here you can see this is the configuration file I currently have for this server so we already have a 443 port which is the secure port SSL is set up on this and you can see the server name is just plain old go blockade without the WW there you go so that's everything you need to do now what we want to do is we want to create a different server block this time we want to create a server block for our unsecure server okay and for the port 80 now the www website in my case because I'm redirecting from ww2 non WW my SSL certificate is on the non WW now this is important to understand because when someone goes to your www website they're going to they're not going to be on the secure port because there is no secure port for that because your asses unless you have an SSL certificate for it as well but which you might there are like wildcard redirects SSL certificates you can have as of right now I think it's coming in 2018 but as of right now if you're using let's encrypt they do not support wildcard redirects so in that case that's what I'm using is let's encrypt so I don't have a wild-card redirect so my SSL certificate is only for go blockade without the WW and that's why it's important to redirect people that are on the unsecured portion the unsecured port using WW or on the unsecured port using the the non www we want to redirect all of those people to the secure port using non ww-want to redirect them here and that's why this is important okay so now let's go ahead and let's fill out this server blog so what we're going to be doing is we're gonna say listen and we're going to tell it what port to listen on to which is gonna be port 80 port 80 is the default unsecured port that most internet traffic goes through unless it's C SSL okay so what we're listening on port 80 and then you can kind of see the format down here we also are going to define a server name and this server name is going to define the the websites that we're going to be redirecting so in our case this is go blockade comm and of course this should go without saying don't put in go blockade comm that's my domain you put in your own domain just note that there's no www so I'm doing the non WW version because I want to redirect and we're in the port 80 port so this is the this is the unsecure non WW version then we're gonna do a space and we're gonna do another website this time WWE my domain comm and so that way it's gonna redirect both of these are going to be treated basically the same as what it means okay go put a semicolon and then for the last thing we're gonna set up a return and this just defines what happens when someone hits that block so whenever someone hits one of these two sites on the unsecured port what do we do we're gonna set up a 301 redirect this is the best for you know SEO and all sorts of stuff so we're gonna set up a one redirect and we're going to tell it to go to our HTTPS the secure version and the we're in this case we're not going to set up the www so we're telling it to go to the secure HTTPS no WW and then my domain last but not least we also want to set up this thing called this variable called request URI and the reason that this is important is because this that request URI variable is gonna represent anything and that's after the domain so if someone goes to WWE blockade comm slash contact we want to redirect them to the secure version but we still want them to go to that con that slash contact page and that slash contact is maintained inside of that request URI variable so what we're saying here is go to this version and then add any request URI they might have they don't have one that it will be empty so it's not a problem now obviously when you set this up make sure you already have another you already need to have a server port or a server block for where you're redirecting them to for this to work but we already have that that's down here ok that's everything that we need let's go ahead and do control X to save down here at the bottom you can see it asks for if we want to save the buffer we say yes then it confirms the file name just click enter and it should go ahead and save that alright now the next thing we need to do is restart nginx ok so the way we're going to handle that I'm gonna use sudo you'll probably need sudo and then we'll just do sudo system control and then we'll do restart nginx ok that's how I do it in in butoh some other times you might be do like services restart nginx or services engine X restart I can't remember the format exactly I'm so used to in Bluetooth so I'm not sure but this is the command for amoun - all right so we read ahead we redirect we restarted our nginx server now we can come over here let's attempt now to go to the www block it should take effect more or less immediately and if you don't have very much on your server it should boot up pretty fast okay so let's try W W as you can see it redirects us straight back to this same website let's try now to go to WWE website slash contact and you can see that it redirected us to the H the secure version non a non WW and added our slash contact so we are good to go there everything is working perfectly now I'm gonna go ahead and exit out of this real quick out of my SSH you can also confirm this by using curl - I capital I and then go to will go to http and then the www version and you can see here we get that 301 redirect is inside of here and you can see that it's go it's getting redirected to our HTTP slash go blockade comm right so we're all ready we can kind of see this as well if I go ahead and do the same one will do slash contact and it redirects us there without this slash contact but you can see we're getting that 301 redirect that's a permanent redirect so that's good for like SEO and stuff like that so that means if someone the reason this is important for SEO is because if somebody were to link let's say someone gives us a backlink for SEO or just someone natively puts in your maybe they're on Twitter they want to tweet out your company's name they may just tweet out simply like go blockade com or they might a lot of times especially older people will put WW because that's what they're used to with the Internet and so they just they'll put in the WW because they just assume they need it and they'll put wwg blockade com and that's the URL they put and so that's going to direct to the unsecured version and the WWV version and we want to get SEO credit for that link someone gave us that link and so having the 301 redirect will make that count alright so that's why that's important that's everything for today's video you guys have now redirected a domain on nginx last thing i will mention for those of you guys that don't know this this should go without saying but again people can get caught up on this you do want to make sure that you've created some sort of cname or a record in your domain registrar whoever's whatever is in your DNS records you need to have a a record for your WW and it needs to point to your server ok and so that's going to be important the reason this is important is because nginx is simply your web server okay so it doesn't determine which subdomains you have you have to tell it which subdomains to use you have to configure it that way so if you haven't created a subdomain for ww3 or dns then the dns is going to caught is going to cause problems it's going to stop before it ever gets to nginx so nginx will never have a chance to redirect it because the DNS servers are going to prevent the request so keep that in mind as well when you're working with this but other than that just make sure you get that wwa I usually use an a record but you can Elsie as a cname and I just do an a record that points over to the same server the same IP address that way when the when the domain gets redirected over to our server nginx now can see it without that record in there the DNS server is going to halt the record before it ever gets to our server before nginx can even deal with it so if I make giving that record nginx can now do what we want with it so make sure you get that in there too this obviously isn't gonna work unless you set up a rec DNS record for this domain ok guys that's everything I got for you guys today hopefully you found this interesting if you guys are new to the channel you just found me through search or anything like that be sure to subscribe there's a lot more content like this on this channel we cover everything from web marketing and web development there's lots of great stuff on here so be sure to subscribe and take a look at some of my other videos until next time guys I'll see you later [Music]
Info
Channel: DevMarketer
Views: 19,755
Rating: undefined out of 5
Keywords: nginx, tutorial, easy, redirect, devmarketer, web, server, ssh
Id: 6QYJUvrb7m8
Channel Id: undefined
Length: 14min 7sec (847 seconds)
Published: Tue Oct 17 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.