<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rewrite code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 02:00:16 GMT</pubDate>
    <description>DZone Snippets: rewrite code</description>
    <item>
      <title>Make sure your site (or directory) is SSL encrypted</title>
      <link>http://snippets.dzone.com/posts/show/4405</link>
      <description>Need a simple way to make sure all http requests get redirected to https?&lt;br /&gt;This apache config snippet will redirect all requests at or below the specified location to its https equivilant.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;Location "/"&gt;&lt;br /&gt;        RewriteEngine on&lt;br /&gt;        Options +FollowSymLinks&lt;br /&gt;        Allow from all&lt;br /&gt;        RewriteCond %{SERVER_PORT} !^443$&lt;br /&gt;        RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]&lt;br /&gt;&lt;/Location&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Aug 2007 17:52:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4405</guid>
      <author>mikehale (Michael Hale)</author>
    </item>
    <item>
      <title>ISAPI URL Rewrite</title>
      <link>http://snippets.dzone.com/posts/show/4225</link>
      <description>// description of your code here&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;windows.h&gt;&lt;br /&gt;#include &lt;httpfilt.h&gt;&lt;br /&gt;&lt;br /&gt;#define MAX_URL_LEN 4096&lt;br /&gt;&lt;br /&gt;BOOL WINAPI GetFilterVersion(HTTP_FILTER_VERSION * pVer)&lt;br /&gt;{&lt;br /&gt;      pVer-&gt;dwFlags = (SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_PREPROC_HEADERS | SF_NOTIFY_ORDER_HIGH);&lt;br /&gt;      pVer-&gt;dwFilterVersion = HTTP_FILTER_REVISION;&lt;br /&gt;      strcpy_s(pVer-&gt;lpszFilterDesc,9,"Blah blah blah");&lt;br /&gt;      return TRUE;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void ReMapURLs(CHAR* pUrl,HTTP_FILTER_CONTEXT* pfc,PHTTP_FILTER_PREPROC_HEADERS pHeaders)&lt;br /&gt;{&lt;br /&gt;      if (pUrl[0] != '/') return;&lt;br /&gt;&lt;br /&gt;      CHAR *iUrl = 0;&lt;br /&gt;      BOOL doSet = FALSE;&lt;br /&gt;&lt;br /&gt;      char *sOldUrls[] = { "/test/", "/TEST/" };&lt;br /&gt;      char *sNewUrls[] = { "/go/", "/GO/" };&lt;br /&gt;&lt;br /&gt;      for (int i=0; i&lt;2; i++)&lt;br /&gt;      {&lt;br /&gt;            if (iUrl = strstr(pUrl,sOldUrls[i]))&lt;br /&gt;            {&lt;br /&gt;                  doSet = TRUE;&lt;br /&gt;                  memcpy(iUrl,sNewUrls[i],strlen(sNewUrls[i]));&lt;br /&gt;            }&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      if (doSet) pHeaders-&gt;SetHeader(pfc, "url", pUrl);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;DWORD WINAPI HttpFilterProc(HTTP_FILTER_CONTEXT *pfc,DWORD NotificationType,VOID * pvData)&lt;br /&gt;{&lt;br /&gt;      PHTTP_FILTER_PREPROC_HEADERS pHeaders;&lt;br /&gt;      DWORD cUrlOrig = MAX_URL_LEN;&lt;br /&gt;      DWORD cUrl = cUrlOrig;&lt;br /&gt;      CHAR rgUrl[MAX_URL_LEN];&lt;br /&gt;      CHAR *pUrl;&lt;br /&gt;      BOOL result;&lt;br /&gt;&lt;br /&gt;      switch ( NotificationType )&lt;br /&gt;      {&lt;br /&gt;            case SF_NOTIFY_PREPROC_HEADERS:&lt;br /&gt;                  pHeaders = (PHTTP_FILTER_PREPROC_HEADERS) pvData;&lt;br /&gt;                  result = pHeaders-&gt;GetHeader(pfc, "url", rgUrl, &amp;cUrl);&lt;br /&gt;&lt;br /&gt;                  if (!result &amp;&amp; cUrl &gt; cUrlOrig)&lt;br /&gt;                  {&lt;br /&gt;                        pUrl = (CHAR*)LocalAlloc(0, cUrl);&lt;br /&gt;                        result = pHeaders-&gt;GetHeader(pfc, "url", pUrl, &amp;cUrl);&lt;br /&gt;                        if (!result)&lt;br /&gt;                        {&lt;br /&gt;                              LocalFree(pUrl);&lt;br /&gt;                              break;&lt;br /&gt;                        }&lt;br /&gt;                        ReMapURLs(pUrl, pfc, pHeaders);&lt;br /&gt;                        LocalFree(pUrl);&lt;br /&gt;                  }&lt;br /&gt;                  else&lt;br /&gt;                        ReMapURLs(rgUrl, pfc, pHeaders);&lt;br /&gt;                  break;&lt;br /&gt;            default:&lt;br /&gt;                  break;&lt;br /&gt;      }&lt;br /&gt;      return SF_STATUS_REQ_NEXT_NOTIFICATION;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 29 Jun 2007 08:01:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4225</guid>
      <author>mornlee (mornlee)</author>
    </item>
    <item>
      <title>Rewrite Apache logs that have incorrect dates</title>
      <link>http://snippets.dzone.com/posts/show/4090</link>
      <description>// Rewrite Apache logs that have incorrect dates&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;&lt;br /&gt;# $Id$&lt;br /&gt;&lt;br /&gt;# Rewrite Apache logs that have incorrect dates.&lt;br /&gt;# Example usage: $0 '28/May/2006:01:17:14 +0200' '19/Jan/2007:08:49:14 +0100' \&lt;br /&gt;#                access_log.* error_log.*&lt;br /&gt;&lt;br /&gt;use strict;&lt;br /&gt;use warnings;&lt;br /&gt;&lt;br /&gt;## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)&lt;br /&gt;our ($VERSION) = '$Revision$' =~ m{ \$Revision: \s+ (\S+) }xms;&lt;br /&gt;## use critic&lt;br /&gt;&lt;br /&gt;use HTTP::Date;&lt;br /&gt;&lt;br /&gt;my @DAYS   = qw(Sun Mon Tue Wed Thu Fri Sat);&lt;br /&gt;my @MONTHS = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);&lt;br /&gt;&lt;br /&gt;my $wrong_datetime = shift @ARGV;&lt;br /&gt;my $right_datetime = shift @ARGV;&lt;br /&gt;&lt;br /&gt;my ($timezone) = $right_datetime =~ m/ ([+-]\d\d\d\d)\z/xms;&lt;br /&gt;&lt;br /&gt;my $seconds_diff = str2time($right_datetime) - str2time($wrong_datetime);&lt;br /&gt;&lt;br /&gt;foreach my $file (@ARGV) {&lt;br /&gt;    print "Rewriting $file\n";&lt;br /&gt;    open my $IN,  '&lt;', $file&lt;br /&gt;        or die "Can't open $file: $!\n";&lt;br /&gt;    open my $OUT, '&gt;', "$file.rewritten"&lt;br /&gt;        or die "Can't write to $file.rewritten: $!\n";&lt;br /&gt;    while (&lt;$IN&gt;) {&lt;br /&gt;        if (m{&lt;br /&gt;               \A&lt;br /&gt;               (.+\s+) # Before date and time (if any)&lt;br /&gt;               \[&lt;br /&gt;               (&lt;br /&gt;                   \d\d/\w\w\w/\d\d\d\d # Date&lt;br /&gt;                   :\d\d:\d\d:\d\d      # Time&lt;br /&gt;                   \s&lt;br /&gt;                   [\+\-]\d\d\d\d       # Time zone&lt;br /&gt;               )&lt;br /&gt;               \]&lt;br /&gt;               (\s+.+) # After date and time&lt;br /&gt;               \z&lt;br /&gt;             }xms) {&lt;br /&gt;            print {$OUT} &lt;br /&gt;              $1, q{[},&lt;br /&gt;              rewrite_access_datetime($2, $seconds_diff, $timezone),&lt;br /&gt;              q{]}, $3;&lt;br /&gt;        }&lt;br /&gt;        elsif (m{&lt;br /&gt;               \A&lt;br /&gt;               \[&lt;br /&gt;               (&lt;br /&gt;                   \w\w\w \s \w\w\w \s \d\d \s # Date&lt;br /&gt;                   \d\d:\d\d:\d\d \s           # Time&lt;br /&gt;                   \d\d\d\d                    # Year&lt;br /&gt;               )&lt;br /&gt;               \]&lt;br /&gt;               (\s+.+) # After date and time&lt;br /&gt;               \z&lt;br /&gt;             }xms) {&lt;br /&gt;            print {$OUT} &lt;br /&gt;              q{[},&lt;br /&gt;              rewrite_error_datetime($1, $seconds_diff),&lt;br /&gt;              q{]}, $2;&lt;br /&gt;        }&lt;br /&gt;        else {&lt;br /&gt;            print {$OUT} $_;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub rewrite_access_datetime {&lt;br /&gt;    my ($datetime, $seconds_diff, $timezone) = @_;&lt;br /&gt;    &lt;br /&gt;    my ($sign, $hours, $minutes) = $timezone =~ m/\A([+-])(\d\d)(\d\d)\z/xms;&lt;br /&gt;    my $seconds_offset = ($hours * 60 + $minutes) * 60;&lt;br /&gt;    &lt;br /&gt;    $datetime = str2time($datetime) + $seconds_diff;&lt;br /&gt;    if    ($sign eq q{+}) {&lt;br /&gt;        $datetime = $datetime + $seconds_offset;&lt;br /&gt;    }&lt;br /&gt;    elsif ($sign eq q{-}) {&lt;br /&gt;        $datetime = $datetime - $seconds_offset;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    my ($sec, $min, $hour, $mday, $mon, $year) = gmtime $datetime;&lt;br /&gt;    return sprintf '%02d/%s/%04d:%02d:%02d:%02d %s',&lt;br /&gt;        $mday, $MONTHS[$mon], $year + 1900, $hour, $min, $sec, $timezone;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub rewrite_error_datetime {&lt;br /&gt;    my ($datetime, $seconds_diff) = @_;&lt;br /&gt;    &lt;br /&gt;    $datetime = str2time($datetime) + $seconds_diff;&lt;br /&gt;    &lt;br /&gt;    my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime $datetime;&lt;br /&gt;    return sprintf '%s %s %02d %02d:%02d:%02d %04d',&lt;br /&gt;        $DAYS[$wday], $MONTHS[$mon], $mday, $hour, $min, $sec, $year + 1900;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jun 2007 07:26:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4090</guid>
      <author>iansealy (Ian Sealy)</author>
    </item>
    <item>
      <title>Redirect all HTTP requests to HTTPS with ISAPI Rewrite</title>
      <link>http://snippets.dzone.com/posts/show/3866</link>
      <description>To redirect all HTTP requests to HTTPS with ISAPI Rewrite you can use the following rewriting rules in your httpd.ini file:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# redirect all http requests  to https&lt;br /&gt;RewriteCond  %HTTPS (?!on).*&lt;br /&gt;RewriteCond Host: (.*)&lt;br /&gt;RewriteRule (.*) https\://$1$2 [I,RP]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Thanks to &lt;a href="http://www.ivanuzunov.net/" title="Ivan Uzunov"&gt;Ivan Uzunov&lt;/a&gt;</description>
      <pubDate>Tue, 24 Apr 2007 08:25:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3866</guid>
      <author>volume4 (Schalk Neethling)</author>
    </item>
    <item>
      <title>replace file extension with apache rewrite</title>
      <link>http://snippets.dzone.com/posts/show/2604</link>
      <description>// in this example *.html is replaced by *.php&lt;br /&gt;// its a redirect [R] and it is is permanent (code is 301)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;RewriteEngine On&lt;br /&gt;RewriteBase   /the_directory&lt;br /&gt;&lt;br /&gt;RewriteRule  ^(.*).html$ $1.php [R=301]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 16 Sep 2006 16:24:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2604</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>rewrite rules</title>
      <link>http://snippets.dzone.com/posts/show/2543</link>
      <description>// if cms has cryptic urls&lt;br /&gt;// in apache url-rewriting with .htaccess can help&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;RewriteRule cms/fest$ cms/index.php?option=content&amp;task=view&amp;id=176&amp;Itemid=202 [NC]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// [NC] = not case-sensitive</description>
      <pubDate>Tue, 05 Sep 2006 04:31:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2543</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
  </channel>
</rss>
