Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Redirect a Website or URL in htaccess Method

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 582
    Comment on it

    htaccess File method:

    Any website running on Apache server you can use htaccess file method also called as Apache redirects. Important point is that it will work only on Linux servers with the mod_rewrite enabled.  

    Now question is that What is this mod_rewrite ?

    mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs.


    Source: mod_rewrite

       

       

    It is most common method of redirection to new website or URL. I personally recommend this method because here all the redirection handles at the server end. and same is re. Your htaccess file reside root folder of your website depending upon type of hosting and other factor it may be in your website's [public_html] or 'httpdocs'. Always save file as ".htaccess" not as 'htaccess.txt'. If you don't have one you can create with the help of any text editor like notepad .htaccess file. Make sure you save in same file in root folder.

     

    htaccess Redirect Old Domain to New Domain

     

    Options +FollowSymLinks RewriteEngine on</code>
    
    RewriteCond %{HTTP&#95;HOST} ^www.OldWebsite.com$ [OR]
    
    RewriteCond %{HTTP&#95;HOST} ^OldWebsite.com$
    
    RewriteRule ^(.*)$ http://www.newebsite.com/$1 [R=301,L]
    

     

    Redirect www to non-www Domain

     

    Add the following code in .htaccess file to permanently redirect all the www addresses to their non www counterparts:

     

    RewriteEngine On
    
    RewriteCond %{HTTP&#95;HOST}  ^www.xyz.com [NC]
    
    RewriteRule ^(.*) http://xyz.com/$1 [L,R=301]
    

    Replace xyz.com with name of your Website.

     

    I have read many blog on Htaccess file redirection but I must admit that, I have never encounter proper explanation of the code and terms used htaccess redirect file. Finally I found it on some Blog Post(Link Given).

     

    Explanation of Htaccess file Code

     

    1. Options +FollowSymLinks is an Apache directive, prerequisite for mod_rewrite.
    2. RewriteEngine On enables mod_rewrite.
    3. RewriteRule defines a particular rule.
    4. The first string of characters after RewriteRule defines what the original URL looks like.
    5. The second string after RewriteRule defines the new URL. This is in relation to the document root (html) directory. / means the html directory itself, and subfolders can also be specified.
      • $1 at the end matches the part in parentheses () from the first string. Basically, this makes sure that sub-pages get redirected to the same sub-page and not the main page. Leave it out to redirect to the main page. (If you don't have the same content in the new directory that you had in the old directory, leave this out.)
    6. [R=301,L] - this performs a 301 redirect and also stops any later rewrite rules from affecting this URL (a good idea to add after the last rule). It's on the same line as RewriteRule, at the end.

    Source: htaccess Rewrite Rules

    Hope This blog post / Tutorial on Apache htaccess redirect URL will help you. If You have any query on Apache, Do post on Apache Forum.

    Apart from This if you have any Java language query, Do post at our Java Questions and Answers Forum.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: