Monday, August 30, 2010

URL Rewriting

Wikipedia definition:
A rewrite engine is a software that modifies a web URL's appearance (URL rewriting). Rewritten URLs (sometimes known as short, fancy URLs, or search engine friendly -SEF) are used to provide shorter and more relevant-looking links to web pages. The technique adds a degree of separation between the files used to generate a web page and the URL that is presented to the world.

How to test it?
  • Install Apache webserver.
  • Create new virtual host(s) in httpd.conf









<VirtualHost *:80>
DocumentRoot /var/www/redhat
ServerName customers.redhat.com
<Directory "/var/www/redhat">
   Options All 
       AllowOverride All 
</Directory>


###################
# ONLY FOR TESTING REWRITE RULES!!!!!
###################
RewriteLog "/etc/httpd/logs/rewrite.log"
RewriteLogLevel 5 
#Level 0-9
  
###################
#Case Studies
###################
RewriteEngine on   # Turn on the rewriting engine
RewriteCond %{HTTP_HOST} customers\.redhat\.com [NC] 
RewriteRule ^(.*)$ http://www.redhat.com$1 [L,R=301]
</VirtualHost>

  • Turn on access filename or add rules to virtual host.
# AccessFileName: The name of the file to look for in each directory

    # for additional configuration directives. See also the AllowOverride

      # directive.

        #

          AccessFileName .htaccess

          • Add new host to hosts files
          127.0.0.1 www.redhat.com
          • Add rules to .htaccess file
          RewriteEngine On # Turn on the rewriting engine
          RewriteRule ^pet-care/?$ pet_care_info_01_07_2008.php [NC,L] # Handle requests for "pet-care"

          http://www.pets.com/pet_care_info_07_07_2008.php


          We want to clean up the URL, and our ideal URL would be:
          http://www.pets.com/pet-care/



          httpd.conf

          Remember, if you put these rules in the main server conf file (usually httpd.conf) rather than an .htaccess file, you'll need to use ^/... ... instead of ^... ... at the beginning of the RewriteRule line, in other words, add a slash.




          Useful Links

          No comments:

          Post a Comment