02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Using Virtual Hosts to redirect www to non-www

Using Virtual Hosts to redirect www to non-www or the other way around.

Video Transcript

00:00Welcome back, and it's our last topic in this first segment about Apache or HTTP D. Don't worry
00:06We're going to learn a lot more about that in the future
00:08But we wanted to close up with this last topic which is about what if you want to have multiple domains or for example in
00:15our case we want to have a
00:18Redirect and the most efficient way to create redirects is through that config file. This is actually the recommended way
00:25So I'm going to show you the way. It's quite simple. We're going to jump right in
00:29I'm just going to go back in to edit our
00:31HTTP D config file
00:33And I'm going to jump right to the bottom of the page click on the insert area now the first step
00:39And this is a critical one, so I'm going to say this twice before I do anything else
00:43I want to declare and basically say that there's more than one
00:47virtual host on this server the way we do that on
00:51Apache is we basically give a name to the virtual host on a global area outside of any folder and basically
00:58But there are stars saying that there's multiple
01:01Virtual hosts so to do that what I'm going to type now is just the words name
01:07Virtual hosts, and I'm going to just put there a star and port 80 basically saying that there's more multiple
01:15Star 80 virtual hosts basically enabling me to put more than one and we could put here a plethora and endless amount of websites
01:23All right, so the first website. We already have is my 10.us and notice that I've changed it to be just my 10.us without a dot-com
01:30Well, sorry without the www
01:32What I want to do now is I want to create another virtual host and the second virtual host will store that
01:39www
01:40W so let's do that virtual host
01:43We're going to close that virtual host and as always we want to always close everything we open
01:51before we do anything else
01:53Now next step is we want to define basically
01:57I'm just going to put here a bit of space. I want to define the server name just as we've done earlier
02:01and
02:03The server name we want to give to at this time is going to be www dot my 10 that us
02:08Now that we said that this sub server or the core server what I really want to say hey anytime you hit this
02:15www
02:16Redirect the user to not have that
02:19Anymore the way we do that is
02:21Simple now by the way usually the standard and what is recommended in most documents is the other way around to do to create your default
02:28to be
02:29www
02:30now the reason behind is very simple because you might have different services on your domain such as
02:36court one that my 10 that us or for example Google as finance that google.com news that google.com and it wants to do different
02:44Permissions for the different locations of the different sites now if you set your site to be my 10.us
02:51Any permission you give to your my site that 10 will automatically be also available for the news
02:59At the US and so on and so forth all these other subdomains would automatically gain access to it
03:05That's why it's recommended to always redirect to your www
03:09Now in your case or my case where this site is exclusively private
03:13And I'm not planning on him having multiple subdomains
03:16I'm not that worried about it
03:18And I just want to have a short queued domain and because of that I'm going to get rid of the
03:22www to make it shorter and more palatable more easy on the eye right so with that said
03:28The next step I'm going to write here is redirect. I want to make this redirect permanent
03:34So I'm going to write their permanent so redirect permanent
03:38What do I want to redirect anything from the backslash anything on and I'm going to put their space and where do I want to?
03:44Redirect it. I want to redirect it to HTTP
03:47My 10 that us
03:50Okay, so I'm basically redirecting with the full qualified path in the second parameter or actually the in this case the fourth or third parameter
03:58More accurately alright, so I just want to make sure that I don't have your any typos
04:02We have a virtual host we close the virtual host
04:05I've said the name of the name server to be www dot my domain contrary to my first virtual host
04:11Which was just my domain dot?
04:14Domain whatever it might be and then in the virtual host the second one also redirecting all the www
04:20Anything from the from this backslash to the new domain basically making sure that everything will move on and by the way it is
04:26True that if you wanted to put here a different rule here saying like for example anyone in a subfolder would go to that
04:32Domain you can do that as well with these virtual host rules
04:36Alright, so that's it
04:38So let me just escape and we're gonna save it clicking the shifts easy and all that's left for me to do is to click
04:44On and restart my server using the service
04:47HTTPD restart and we're gonna stop the service and once I'm we have your end issues
04:51We're gonna have to fix it first invalid first argument of three and our redirect basically meaning that we probably have a typo there
04:59So let me jump right in there really quickly and let me just make sure that in my the word
05:03Permanent that I'm actually typing it correctly. So let me just click on I for insert again
05:08Let me just rewrite it. So I'm gonna make sure that I write it correctly this time around which was with an er first
05:14ma n e n t instead of what I had there earlier
05:18So again, I'm gonna click on escape shift is easy
05:21I'm gonna try and restart again and hopefully we don't have a problem by the way
05:25don't worry about when you see in the stop when it fails because
05:29Before when I did a restart it failed to start up. So now when I'm doing a restart
05:34It's just telling me that it actually wasn't running. So now when it's starting at work, that's that's good
05:38That's all I really care about
05:39So let's jump really really quickly and into our browser and I'm gonna go ahead and try to go directly
05:45Instead of to my and I'm just gonna paste it here instead of my 10.us. I'm gonna try and go directly to
05:50www.my 10.us and I'm expecting it to redirect as you can see
05:55It's redirecting and taking our user to my 10.us
06:00And there you go. That's all we had to do to create a redirect and actually add multiple hosts
06:05So in any case if you were gonna create another host you're ready now are prepared and ready and you could literally
06:11Create these both you want to make sure that you consolidate the WW we explained about why
06:16Usually you probably want to have your main folder to be a www one
06:20But this is really to your consideration as I've talked about earlier
06:24But just now it right now you could continue and adding as many
06:28Virtual hosts that you want to need and you have all the information you need to be able to add as many of virtual hosts that
06:34You would want so that's it. That's all of our conversation about Apache the server and in the next few videos
06:41We're gonna continue on continuing the journey through lamp in general where we already discovered the basics of Linux
06:48We discovered the basics of Apache and we're gonna move on to discover the basics of
06:52Setting up PHP and MySQL. So see you in the next module

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!