Redirect To a Different URL Using .htaccess Configuration
This is the third part of the .htaccess tutorial series. In this tutorial I will discuss about the redirects using .htaccess file configuration. Redirect rules help us in redirecting a visitor from one document to another URL within or out side of your website. For example if you have stopped serving from your old directory and want to redirect the visitor to new page which notifies “This service has been discontinued”. So let us see how to redirect to a different url.
Redirect To Different URL
So let’s see how you can redirect to a different URL using .htacces file configuration. Below is the configuration settings that you need to do. Make sure that you do the changes to the root level .htaccess file.
Redirect /MyDir/ http://www.yourdomain.com/service-discontinued.html
In the above configuration we are telling the apache server that if any visitor requests any document under the directory “MyDir”, then redirect them to the URL http://www.yourdomain.com/service-discontinued.html.
If you note here the second part of the configuration is a complete URL to which we are redirecting and not a relative path on your web server. So always make sure that whenever you are doing this configuration, the target part should be a complete URL and not a relative path. Another thing is, as the target part is a complete URL you can specify any valid URL in it, it can be within your domain or outside your domain.
So now let’s generalize the redirect configuration for .htaccess:
Redirect source-relative-path destination-complete-URL
So the source-relative-path should be replaced with the exact relative path to the document and destination-complete-URL should be replaced with the complete absolute URL of the target.
Warning: Syntax in the .htaccess file configuration are case sensitive and can cause internal server error if a single syntax is even wrong. So always be careful while you are editing the .htaccess file and make sure you maintain the proper cases.