|
.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking | |
|
.htaccess - Apache directory-level configuration file |
Redirect
The htaccess command Redirect is very powerful you can redirect files or complete folders. Why would you want to do this a typical example would be that you have changed the structure of your site but wish old links (saved by a user) to be redirected to your new page location or even new page name.
The redirect command must be on a single line with each element separated by a space it has the following format:
Looking at this in a little more detail using this example
Redirect /olddirectory/oldfile.html http://yoursite.com/newdirectory/newfile.html
You would place a Redirect command in the root htaccess file for each file you want to redirect.
Suppose that all you have done is renamed a folder (and even moved it to another location) however all the files still have their original names redirecting a folder is similar to the above for example:
Redirect /olddirectory http://yoursite.com/newdirectory
Using our errors folder make a copy of it and rename it to moved.
| Comment | Code |
|---|---|
|
# This file provides security to the server limiting access to the localhost only. # Comment to deactivate. Order Deny,Allow Deny from all Allow from 127.0.0.1 # To allow execution of cgi scripts in this directory uncomment next two lines. AddHandler cgi-script .pl .cgi Options +ExecCGI # To unlock your server, comment the next 4 lines. # Defaults: Username = admin; Password = userver #AuthName "Uniform Server - Secure Server Access" #AuthType Basic #AuthUserFile /htpasswd/www/.htpasswd #Require valid-user Redirect /old http://localhost/moved |
Testing
Open the file not_found.html contained in folder moved add some more text to distinguish it from the original.
Testing
Well you get the idea experiment with files it is similar to the folder example.
I was faced with this very problem UniCenter has been around for nearly two years and started to become unmanageable hence the move over to the Wiki. Many web sites link to various pages on UniCenter to avoid frustration of not finding an article; users are redirected to the appropriate page on the Wiki.
Below is a small extract from my .htaccess file you will see I am using page redirection:
Redirect /us_35_apps/joomla/joomla_install_1.html http://wiki.uniformserver.com/index.php/Installing_Joomla Redirect /us_35_apps/joomla/joomla_install_2.html http://wiki.uniformserver.com/index.php/Joomla_Portability Redirect /u35_mod_ssl_2/4_hmailserver.html http://wiki.uniformserver.com/index.php/SSL_Part_2:_Installing_hMailServer Redirect /u35_mod_ssl_2/5_hmailserver.html http://wiki.uniformserver.com/index.php/SSL_Part_2:_Configure_hMailServer Redirect /new_users/new_users_index.html http://wiki.uniformserver.com/index.php/New_Users:_Home Redirect /new_users/new_users_install_1.html http://wiki.uniformserver.com/index.php/New_Users:_Quick_Install_Guide Redirect /new_users/problems_1.html http://wiki.uniformserver.com/index.php/New_Users:_Problems_Section
Tip: I first publish the Wiki pages, use a browser to view a page and copy the link into .htaccess. Similar process to obtain the redirected page.
Any typos in the .htaccess file will cause Apache to spit out the following error message:
Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.
Hence always make small changes to .htaccess and then test, makes it easier to isolate and resolve any problems.
| | Ric |