This tutorial deals with how you can redirect one URL (ie, web address) to another, so that if a
visitor goes to (say) http://www.example.com/old-page.html
, he/she
is automatically redirected to http://www.example.com/new-page.html
.
The original URL must be on a website that you control (for example, it belongs to you). You cannot redirect some random web address on the Internet to another. The destination URL, however, can be anywhere you like.
Your website must be on a machine running the Apache web server software. We will be modifying one of the
server's configuration files, specifically, .htaccess
.
Your web host must allow you to modify the server settings through the use of .htaccess
directives.
Most commercial web hosts permit
this.
To direct one URL to another, for example, from http://www.example.com/old-page.html
to
http://www.example.com/new-page.html
, all that is needed is to add a line like the one below
to your .htaccess
file located in the directory (folder) of the original page. (Don't panic
if you didn't understand the bit about the .htaccess
file. I'll explain in greater detail in
the paragraphs that follow.)
The words "Redirect permanent" tells search engines and browsers that the address,
http://www.example.com/old-page.html
has permanently changed to
http://www.example.com/new-page.html
. You can also use "Redirect 301"
in place of "Redirect permanent" if you prefer. They mean the same thing. "301" is the
numeric status code the server sends to the browser, and it means "permanent".
Notice that you cannot specify the full URL of the old page. Just state the part after
your domain name.
So, if the page to be redirected is http://www.example.com/old-page.html
, you should
only say /old-page.html
. However, the destination of the redirection can be any URL,
even one that is on some other website.
If the redirection is temporary for some reason (for example, it's only for a day while you update
old-page.html
), use the following directive instead:
You can also say "Redirect 302" instead of "Redirect temp". "302" is the numeric status code for a temporary redirection and has the exact same meaning as the "temp" directive given above.
If your original file is in a subdirectory and not the main directory of your website, include the subdirectory name, as follows.
The above redirects all requests for http://www.example.com/offers/winter-specials.html
to
http://www.example.com/expired-offers.html
. The "offers" directory must be specified in
the directive even though the .htaccess
file is already in the "offers" folder.
Be sure to hit the ENTER key (or the RETURN key on a Mac) after the directive, so that the file ends on a blank line. That is, when you move your text cursor to the end of the file in the editor, the cursor should be on a blank line, and not the tail end of the line you just typed.
First connect to your website using an FTP program. If you don't have one (perhaps because you created your site using a visual web editor that did everything for you), see my brief tutorial on FileZilla for information on how to get and use one such software. FileZilla is one of many free FTP programs around.
Go to the directory (ie, folder) of the original file (for example, the directory containing old-page.html
),
and look for any .htaccess
file there.
If you use FileZilla, and you don't see any .htaccess
file, you may have to click "Server" on the menu bar,
and select the line "Force showing hidden files" [sic] in the drop-down menu that appears to put a tick there.
If the file is already visible in the directory, you don't need to enable this setting.
If there is an existing .htaccess
file, download it to your computer. If you don't see any, and you
are sure your FTP software is set up to display all hidden files, don't worry. The .htaccess
file doesn't
exist by default. It will only be there if you have created one before or if your web host has put one there. If none
exists, you will make a new one in the next section.
Whether you are creating a new .htaccess
file or editing an existing one, you will need to
use a plain text editor, also known as an ASCII text editor. Windows users already have one in the form of a program
called Notepad. Just click the Start menu, type "notepad" and hit the ENTER key. I'm told that Mac OS X users
also have one on their system, called TextEdit. Alternatively, you can also get one of the programs
listed on the Free
Programmer's Editors page. Don't be intimidated by the grandiose name of "programmer's editors" — they are
just plain text editors under the hood.
Under no circumstances should you use a word
processor like Microsoft Word, WordPad, Write, Open Office, LibreOffice, or the like. If the program
you're using allows you to underline words or put them in bold or italics, it is the wrong software.
It will corrupt your .htaccess
file, and render your website inaccessible.
Open .htaccess
in the editor, and type the appropriate directive on a new line. Remember to change
the URLs in my examples so that they refer to the ones you want. If you don't have an existing .htaccess
file, just type the line into a new blank document.
Save the file as ".htaccess"
. Make sure the filename begins with a full stop (ie, period). If you use Notepad,
make sure you type ".htaccess"
including the quotation marks, or it will
append a
".txt" extension to the filename behind your back, saving it as ".htaccess.txt
". Even worse,
you won't be able to spot this annoying change unless you have also
configured
Windows to show you the file extensions, so make sure you don't forget the quotation marks.
Once you're done, upload the file back to your site, placing it in the directory of the original file (that is,
the same folder where old-page.html
lives, in the case of our example above). If your FTP software
needs you to specify the mode in which to upload, transfer the file in ASCII mode (not binary). Most software (including
FileZilla) should automatically do this correctly, unless you have messed with the default settings.
Now test that your redirection works by typing your old URL into the browser. You should automatically be sent to the
new one. I also recommend that you test the URLs of some of the other pages in the directory where you placed the
.htaccess
file, to make sure that you didn't accidentally make an error that renders those
pages inaccessible. That is just go to some of those pages in your browser to make sure that they still load
properly.
If you can no longer access your site after uploading the .htaccess
file, it means you have somehow
corrupted the latter. One possible reason is that you used a word processor to work on that file.
Another is that you made a typing error, or perhaps failed to make the file end on a blank line.
Or you could have uploaded the file from a Windows computer in binary mode instead of ASCII.
To fix it, delete the .htaccess
file on your website. The site should now return to normal.
However, all the directives specified in that file will be gone, and you will have to recreate it.
This time, be sure to follow the instructions given above meticulously and double-check your typing.
Copyright © 2017-2018 Christopher Heng. All rights reserved.
Get more free tips and articles like this,
on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
It will appear on your page as: