PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: How to redirect the urls using .htaccess

26 Nov 2013

How to redirect the urls using .htaccess

                        You can redirect the urls from one file name to another. It is possible in .htaccess. It can be done by 'RewriteRule' in .htaccess. The 'RewriteRule' is used to redirect the urls from one path to another. You can make your site urls very simple using RewriteRule method.


Rewrite urls using .htaccess:

                       You can rewrite the urls using .htaccess. When your address bar gets one page, the .htaccess can run another page. The .htaccess rewrite the urls from one page to another. Consider the following example.
Syntax:
                RewriteRule ^ex1.php example.php [L]

Explanation:
                First Rule:
                    when the ex1.php file is going to run, .htaccess file runs the example.php instead of ex1.php.

Hide file extension like php, html using .htaccess:

                             You can hide extensions like .php and .html using .htaccess. Consider the following  example.
                       
Syntax:
                RewriteRule ^home home.php [L]

Explanation:
                    When the browser get home to run, .htaccess runs the home.php instead of home. In this method, you can hide your file extensions like .html, .php .

Remove query string using .htaccess:

                               You can remove query string in address bar using .htaccess. There you can pass values in address bar. Consider the following example.


Syntax:
               RewriteRule ^example/(.*)$ example.php?ex=$1 [L]
Explanation:          
                     it avoids the normal rule.
      ie, example.php?ex=something
                 where,
                           when the browser get example.com/example/something, it considered as example.com/example.php?ex=something. Then you can get this value by $_GET['ex']. In this method, you can make your urls very simple and it is easily remembered by any browser.
                            ^  - starting point
                            .* -  one or more characters
                            $  - end point
                            L  - tells the server to stop trying to rewrite a URL after it has applied that rule.


How to redirect one web page url to another website:


                            Suppose you may have one more sites. If you feel when the users try to access the particular file, it directs to another your website.You can also redirect the urls from one website to another. It can be possible in .htaccess.

Syntax:
                Redirect  302 / site2.php  http://www.example2.com
Explanation:        
                    Where,  302 is the temporary redirect.
                                  When the user access the site2.php file in your current domain, it redirect to example2.com website.    
                         

How to redirect from one site to another:

                             
                                    Suppose you feel when the user access your one site, it redirect to another site. Then it can be done by .htaccess.

Syntax:
              Redirect 302 /   http://www.example2.com

              Now if the user come to your current domain, it will redirect to example2.com.


Related Post:

No comments:

Post a Comment