Let’s say you have changed the location of one page to another. Since the url for the page has changed, when some other tries to reach that page via Search Engine or a Bookmark, they would get a HTTP 404 (Page not found). This is not good use experience and would eventually result in traffic loss. In order to prevent this, we can setup a redirect at the Web Server level to seamless go to the new url when some one clicks the old url in their browser.This redirect can handled at the Apache Server Level by adding the ReWriteRule in the httpd.conf file of the server(normally located under /etc/httpd/conf) or in the .htaccess file (located in your web root public_html).
Any changes done in httpd.conf needs your server to be restarted for your changes take effect. However if you add in .htacess, these changes are applied immediately, so pickup either of the option what ever works for you.
Scenario – Single Page URL has changed
http://www.yourwebsite.com/old-url has not moved to a new location http://www.yourwebsite.com/new-url.
RewriteRule ^/old-url$ http://www.yourwebsite.com/new-url [R=301,L]
Scenario – Directory as been renamed
http://www.yourwebsite.com/media/* has not moved to a new location http://www.yourwebsite.com/games/*.
RewriteRule ^/media/(.*)$ http://www.yourwebsite.com/games/$1 [R=301,L]
Scenario – Website has move to another domain (yournewwebsite.com)
RewriteRule ^/$ http://www.yournewwebsite.com/$1 [R=301,L]
Let’s look at the syntax of Rewrite url that is used here for direction. 301 means that the Apache Server will send a Http Status code 301 to browser along with the new url. The browser then would make the Http request to the new URL.The L in the rule signifies this is last rule, don’t evaluate any more and perform the redirect as specified in the rule. These rules should be added before any other rules in order for this to work.
Before you use the RewriteRule directive, make sure the directive to load the RewriteRule is in the config file.
LoadModule rewrite_module modules/mod_rewrite.so[pb_builder]