Htaccess: Redirect: Difference between revisions
(Punctuation and grammatical changes; some clarification.) |
(Moved to new category; Additional grammar and cleanup edits.) |
||
Line 2: | Line 2: | ||
<div style="padding:0;margin:0; border-bottom:3px inset #000000"> | <div style="padding:0;margin:0; border-bottom:3px inset #000000"> | ||
{| | {| | ||
.htaccess: | .htaccess: | ||
[[Htaccess: Introduction | Introduction]] | | [[Htaccess: Introduction | Introduction]] | | ||
Line 20: | Line 20: | ||
'''Redirect''' | '''Redirect''' | ||
The Redirect .htaccess command is very powerful. You can redirect files or complete | The Redirect .htaccess command is very powerful. You can redirect files or complete directories. Why would you want to do this? A typical example would be that you have changed the structure of your site but want to have old links (saved by a user) still function and be redirected to your new page location, or even a new page name. | ||
== Redirect format == | == Redirect format == | ||
Line 44: | Line 44: | ||
You would place a Redirect command in the root .htaccess file for each file you want to redirect. | You would place a Redirect command in the root .htaccess file for each file you want to redirect. | ||
=== Folder Redirect === | === Directory (Folder) Redirect === | ||
Suppose that all you have done is to rename a | Suppose that all you have done is to rename a directory (and possibly moved it to another location); however all the files still have their original names. Redirecting a directory is similar to the above; for example: | ||
'''Redirect /olddirectory <nowiki>http://yoursite.com/newdirectory</nowiki>''' | '''Redirect /olddirectory <nowiki>http://yoursite.com/newdirectory</nowiki>''' | ||
Line 60: | Line 60: | ||
== Directory example 1 == | == Directory example 1 == | ||
Using our '''errors''' | Using our '''errors''' directory, make a copy of it and rename it to '''moved'''. | ||
{| | {| | ||
Line 68: | Line 68: | ||
|-style="background:#f9f9f9" | |-style="background:#f9f9f9" | ||
| | | | ||
# Navigate to the root | # Navigate to the root directory '''www'''. | ||
# Open the '''.htaccess''' file | # Open the '''.htaccess''' file | ||
# Add the command shown on the right last line. | # Add the command shown on the right last line. | ||
Line 101: | Line 101: | ||
'''Testing''' | '''Testing''' | ||
# | # (Re)start the server. | ||
# In the browser address bar, type '''<nowiki>http:/localhost/old</nowiki>''' | # In the browser address bar, type '''<nowiki>http:/localhost/old</nowiki>''' | ||
# You will be redirected to the | # You will be redirected to the directory '''<nowiki>http://localhost/moved/</nowiki>''' | ||
== Directory example 2 == | == Directory example 2 == | ||
Open the file '''not_found.html''' contained in the | Open the file '''not_found.html''' contained in the directory that was moved and add some more text to distinguish it from the original. | ||
'''Testing''' | '''Testing''' | ||
# | # (Re)start the server. | ||
# In the browser address bar type '''<nowiki>http:/localhost/old/not_found.html</nowiki>''' | # In the browser address bar type '''<nowiki>http:/localhost/old/not_found.html</nowiki>''' | ||
# You will be redirected to the file '''<nowiki>http://localhost/moved/not_found.html</nowiki>''' | # You will be redirected to the file '''<nowiki>http://localhost/moved/not_found.html</nowiki>''' | ||
And so forth. Experiment with files; it is similar to the directory example. | |||
== Practical Example == | == Practical Example == | ||
After UniCenter had been around for nearly two years, it started to become unmanageable, hence the move over to the Wiki. Many web sites linked to various pages on UniCenter, so to avoid the frustration of not finding an article, users were redirected to the appropriate page on the Wiki. | |||
Below is a small extract from | Below is a small extract from a UniCenter .htaccess file, where you will see the use of page redirection: | ||
<pre> | <pre> | ||
Line 144: | Line 144: | ||
---- | ---- | ||
[[Category: | [[Category: Apache Configuration]] | ||
Latest revision as of 11:44, 21 June 2013
.htaccess - Apache directory-level configuration file |
Redirect
The Redirect .htaccess command is very powerful. You can redirect files or complete directories. Why would you want to do this? A typical example would be that you have changed the structure of your site but want to have old links (saved by a user) still function and be redirected to your new page location, or even a new page name.
Redirect format
The redirect command must be on a single line with each element separated by a space. It has the following format:
- Redirect old_path new_path
File Redirect format
Let's look at this in a little more detail using this example:
Redirect /olddirectory/oldfile.html http://yoursite.com/newdirectory/newfile.html
- Redirect
- This is the Apache command followed by a space
- /olddirectory/oldfile.html
- This is the path to the old file you want redirected
- It is stated in a relative file format
- Followed again by a space
- http://yoursite.com/newdirectory/newfile.html
- This is the full (URL) path of the location you want the request redirected to
You would place a Redirect command in the root .htaccess file for each file you want to redirect.
Directory (Folder) Redirect
Suppose that all you have done is to rename a directory (and possibly moved it to another location); however all the files still have their original names. Redirecting a directory is similar to the above; for example:
Redirect /olddirectory http://yoursite.com/newdirectory
- Redirect
- This is the Apache command followed by a space
- /olddirectory
- This is the path to the old directory you want redirected
- It is stated in a relative file format
- Followed again by a space
- http://yoursite.com/newdirectory
- This is the full (URL) path of the location you want the request redirected to
Directory example 1
Using our errors directory, 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 Allow from ::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
- (Re)start the server.
- In the browser address bar, type http:/localhost/old
- You will be redirected to the directory http://localhost/moved/
Directory example 2
Open the file not_found.html contained in the directory that was moved and add some more text to distinguish it from the original.
Testing
- (Re)start the server.
- In the browser address bar type http:/localhost/old/not_found.html
- You will be redirected to the file http://localhost/moved/not_found.html
And so forth. Experiment with files; it is similar to the directory example.
Practical Example
After UniCenter had been around for nearly two years, it started to become unmanageable, hence the move over to the Wiki. Many web sites linked to various pages on UniCenter, so to avoid the frustration of not finding an article, users were redirected to the appropriate page on the Wiki.
Below is a small extract from a UniCenter .htaccess file, where you will see the use of 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 published the Wiki pages. Then I used a browser to view a page and copy the link into .htaccess. The process is similar to obtaining the redirected page.
Errors
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.
Remember to always make small changes to .htaccess and then test. This makes it easier to isolate and resolve any problems.