www.websiteadministrator.com.au/articles/301redirection.html
Header

Website Administrator

    delivering administrator solutions for your website...

 


Date added: 18 April 2010
Last Mod: 18 April 2010
© 2010 Steve Campisi

Website Administrator Know How Articles

.htaccess: Redirecting the Directory (apache)


The website administrator may occasionally find that he will need to redirect the entire contents of a directory, "301" or "302" style to a new target directory.

The apache status 301 is used to indicate a permanent redirect status

The apache status 302 is used to indicate a temporary redirect status

The .htaccess file allows the Website Administrator to target an incoming request for an old resource address and target that request to the new location of that particular sought after resource. To do this, we first test that your apache installation has the rewrite module installed by using the "IfModule" command. This enables us to ensure the required module is present before carrying out the full command. When that incoming call is made on our resource that was previously located with-in our targeted area, then we can then advise a 301 status and then serve the corrected file from the new location of this moved resource.

apache 301 examples

The following are examples of the source code the Website Administrator we be required to insert into the .htaccess file to enable the redirection of the target file/directory/folder.

This following code is to change *.* calls from a previous directory to the html root of your website. i.e.
www.MyWebsite.com/OldDirectory/
will now become
www.MyWebsite.com/


<IfModule mod_rewrite.c>
RewriteEngine ON

redirect 301 /OldDirectory/ http://www.MyWebsite.com/

</IfModule>

And so it follows that if you wished to move to an alternative directory, this would be the code to use.


<IfModule mod_rewrite.c>
RewriteEngine ON

redirect 301 /OldDirectory/ http://www.MyWebsite.com/NewDirectory/

</IfModule>

www.MyWebsite.com/OldDirectory/somefile.html
can now be found at
www.MyWebsite.com/NewDirectory/somefile.html

and of course the website administrator would ensure he actually did move the target file into that new location so that it can be found. Also note that the first location does not include "http://www.".

And it is assumed the website administrator is bright enough to realize that if he wanted to change the target location of a single file, or point an old file name to a new file name, this information would become part of the rewrite command like in this example:


<IfModule mod_rewrite.c>
RewriteEngine ON

redirect 301 /OldDirectory/old.html http://www.MyWebsite.com/NewDirectory/new.html

</IfModule>

This will work for you if your rewrite module is installed and you have .htaccess privileges. And that concludes our simple 301 lesson.
-Fin


 
Similar Articles: Website Administrator Tips - target-new
 
 
| downloads | home | articles index |