Breaking News
Home / Sys Admin / Sysadmin Tutorials / Setup URL Redirection in Apache

Setup URL Redirection in Apache

Apache URL RedirectLet’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).

How to Redirect URL via Apache

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.

Redirect Single URL Scenario – Single Page URL has changed

For Example

http://www.yourwebsite.com/old-url has not moved to a new location http://www.yourwebsite.com/new-url.

RewriteEngine on

RewriteRule ^/old-url$ http://www.yourwebsite.com/new-url [R=301,L]

Redirect Whole Directory of Pages Scenario – Directory as been renamed

http://www.yourwebsite.com/media/* has not moved to a new location http://www.yourwebsite.com/games/*.

RewriteEngine on

RewriteRule ^/media/(.*)$ http://www.yourwebsite.com/games/$1 [R=301,L]

 

Redirect Website to another Domain Scenario – Website has move to another domain (yournewwebsite.com)

RewriteEngine on

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]

Advertisment ad adsense adlogger