Thursday, May 22, 2008

Example for Mod RewriteRule

#--MOD Rewirite--
RewriteEngine On
RewriteRule ^dir/(.*) /home/httpd/vhosts/jyvepresence.com/httpdocs/index.php?status=$1

#-----------------------
# URL will be
# http://www.domain.com/dir/vikrant
# It will execute index.php
# $1 == vikrant
#------------------------

RewriteRule ^/?([^/]*\.png|[^\./]*)[:;,\.]*$ /home/httpd/vhosts/jyvepresence.com/httpdocs/index.php?status=$1 [L,NS]

#-----------------------
# URL will be
# http://www.domain.com/vikrant.png OR http://www.domain.com/vikrant
# It will execute index.php
# $1 == vikrant.png OR $1 == vikrant
#------------------------

2 -------------------------------------
Old dynamic url:
Code:
www.domain.com/catalog.php?cat=widgets&product_id=1234
New static url:
Code:
www.domain.com/catalog/widgets-1234.html
Code:
#start .htaccess code
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^cat\=([^&]+)\&product_id\=([^&]+)$
RewriteRule ^$ /catalog/%1-%2.html [R=301,L]


Reg Expression notes:
[^&]+ mean find any character except the "&" since it is what seperates the variables in a string. you can back reference matches in a rewrite condition using ()'s just like your rewrite rules but to call them you have to use a % instead of a $.

Benefits:
1. You don't have to hand write 1,000's of 301 redirects

2. Spiders can easily pick up the 301 and pass the scores and index the new urls much faster than having to crawl the entire site over from scratch.

3. Users clicking old dynamic urls in the SERPS will not get a 404 error. They'll go straight to the new static urls.

3 -----------------------------

Browser Dependent Content


Description:

At least for important top-level pages it is sometimes necessary to provide the optimum of browser dependent content, i.e. one has to provide a maximum version for the latest Netscape variants, a minimum version for the Lynx browsers and a average feature version for all others.
Solution:

We cannot use content negotiation because the browsers do not provide their type in that form. Instead we have to act on the HTTP header "User-Agent". The following condig does the following: If the HTTP header "User-Agent" begins with "Mozilla/3", the page foo.html is rewritten to foo.NS.html and and the rewriting stops. If the browser is "Lynx" or "Mozilla" of version 1 or 2 the URL becomes foo.20.html. All other browsers receive page foo.32.html. This is done by the following ruleset:

RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.*
RewriteRule ^foo\.html$ foo.NS.html [L]

RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12].*
RewriteRule ^foo\.html$ foo.20.html [L]

RewriteRule ^foo\.html$ foo.32.html [L]

4-----------------------------------------------------

Simple Example

Just to help illustrate how you could use this information let's assume we had 3 web pages:

http://www.desilva.biz/wd_010427.php
http://www.desilva.biz/grafix_index.php
http://www.desilva.biz/hw_010506.php

and we want to re-direct our readers visiting each page above to their new versions at:

http://www.desilva.biz/refresh.html
http://www.desilva.biz/grafix.html
http://www.desilva.biz/cpuspeed.html

.htaccess

RewriteEngine on
RewriteRule ^wd_010427\.php$ refresh.html [R=301,L]
RewriteRule ^grafix_index\.php$ grafix.html [R=301,L]
RewriteRule ^hw_010506\.php$ cpuspeed.html [R=301,L]

No comments: