Monday 4 March 2013

Htaccess and Modrewrite



# How to change extension through htaccess?
RewriteRule ^viewnews/([0-9]+)/(.*)$ viewnews.php?news_id=$1 [L]
RewriteRule ^gallery/(.+)/(.+)/([0-9]+)$ gallery.php?cat_id=$1&page=$2&list=$3 [L]


# URL are case sensitive?
In Windows, links will get to your pages regardless of capitalization (or lack of it). So if the search engine indexed your page when it was www.example.com/page01.htm, it will still find it even if you renamed it www.example.com/PAGE01.htm (but read on, as there ARE ways that capitalization might effect you). In a Linux or Unix-based environment, things are a little different. The good news is that the base URL (www.example.com) will resolve correctly regardless of capitalization. The (potentially) bad news is that the other pages will not. So if your site is hosted in a Linux/Unix environment and you rename your page www.example.com/PopularPage.htm to www.example.com/popularpage.htm. 


            
Advantage/ Uses of htaccess?
1)   Authorization, authentication: .htaccess files are often used to specify the security restrictions for the particular directory, hence the filename "access". The .htaccess file is often accompanied by an .htpasswd file which stores valid usernames and their passwords.
  
2)   Customized error responses: Changing the page that is shown when a server-side error occurs, for example HTTP 404 Not Found.
Example : ErrorDocument 404 /notfound.html

3) Rewriting URLs: Servers often use .htaccess to rewrite "ugly" URLs to shorter and prettier ones.

4)   Cache Control: .htaccess files allow a server to control User agent caching used by web browsers to reduce bandwidth usage, server load, and perceived lag.
  

# RewriteRule And RewriteCond
The order of rules in the ruleset is important because the rewrite engine processes them in a particular order, as follows:
The rewrite engine loops through the rulesets (each ruleset being made up of RewriteRule directives, with or without RewriteConds), rule by rule. When a particular rule is matched, mod_rewrite also checks the corresponding conditions (RewriteCond directives). For historical reasons the conditions are given first, making the control flow a little bit long-winded.

First the URL is matched against the Pattern of a rule. If it does not match, mod_rewrite immediately stops processing that rule, and goes on to the next rule. If the Pattern matches, mod_rewrite checks for rule conditions. If none are present, the URL will be replaced with a new string, constructed from the Substitution string, and mod_rewrite goes on to the next rule.


RewriteRule: Defines rules for the rewriting engine.
RewriteCond: Defines a condition under which rewriting will take place.
The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive.

Example:
To rewrite the Homepage of a site according to the "User-Agent" header of the request, you can use the following:
 
RewriteCond  %{HTTP_USER_AGENT}  ^Mozilla
RewriteRule  ^/$                 /homepage.max.html  [L]
RewriteCond  %{HTTP_USER_AGENT}  ^Lynx
RewriteRule  ^/$                 /homepage.min.html  [L]
RewriteRule  ^/$                 /homepage.std.html  [L]

No comments:

Post a Comment