I make it a policy of mine that if three people in the same week ask me about something, that I should just make a blog post about it. This week’s apparent hot topic is HTTP redirection; there are tonnes of ways to do it and I’ve just been giving out a link about 301 redirects in various languages. Most people want to go with .htaccess method.
Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]Please REPLACE domain.com and www.domain.com with your actual domain name.
The website’s a little out of date though; it no longer has to be a Linux server (did it ever have to be..?), so long as you have mod-rewrite enabled in your Apache conf file, you’ll be fine. Like the quote says, this just makes sure that anyone that comes to your website is using the www subdomain. (Sidenote, if you have a choice of no sub-domain vs. “www.”, go without the sub-domain.) So, someone going to “neopets.com” will instantly (without them even seeing a page refresh) be taken to “www.neopets.com”.
That page doesn’t give much of an example of what all that code is doing though, so I’ll go through it here so that you can modify it better for your own use.
The “Options” line is an options directive, allowing you to set pretty much all of the options you can set in Apache conf files (so long as you have the right permissions; also, higher up directories override subdirectories). Here, we’re telling Apache that we want to follow system links (links to other directories, even though they’re no physical, like a shortcut). Your redirects most likely won’t fall apart if you don’t have that line.
The rewrite engine is where the awesome happens. The next line just turns it on.
The “rewritecond” is the evaluative part of the redirect. It checks to see if the URL that the user is looking at is the same as it’s argument. You can get the syntax for that evaluation from the rewrite engine’s man page, but essentially it’s just:
rewritecond %{variable_name} patterntocheckagainst
It uses a regex argument. If you don’t know regex, you’ll probably want to go and look at some tutorials first.
The [nc] at the end just makes the URL case insensitive. You can have multiple possible rewriteconds by using [or], and having another condition on the next line.
Last is the rewriterule, syntax here. Again though, it’s regex. It’s a useful skill for any one that codes in anyway, so you may as well learn it.
Again, there are flags, wrapped in square brackets. The [r=301] is telling the browser (and more importantly search engines) what type of redirect it’s doing; 301 is a permanent redirect. It’s just a HTTP status code, make sure you pick the right one. 307 is a temporary redirect.
If it’s still a bit over your head though, you could always contact me and get me to do it.