Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Make sure your site (or directory) is SSL encrypted

Need a simple way to make sure all http requests get redirected to https?
This apache config snippet will redirect all requests at or below the specified location to its https equivilant.

<Location "/">
        RewriteEngine on
        Options +FollowSymLinks
        Allow from all
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Location>

ISAPI URL Rewrite

// description of your code here
#include <windows.h>
#include <httpfilt.h>

#define MAX_URL_LEN 4096

BOOL WINAPI GetFilterVersion(HTTP_FILTER_VERSION * pVer)
{
      pVer->dwFlags = (SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_PREPROC_HEADERS | SF_NOTIFY_ORDER_HIGH);
      pVer->dwFilterVersion = HTTP_FILTER_REVISION;
      strcpy_s(pVer->lpszFilterDesc,9,"Blah blah blah");
      return TRUE;
}

void ReMapURLs(CHAR* pUrl,HTTP_FILTER_CONTEXT* pfc,PHTTP_FILTER_PREPROC_HEADERS pHeaders)
{
      if (pUrl[0] != '/') return;

      CHAR *iUrl = 0;
      BOOL doSet = FALSE;

      char *sOldUrls[] = { "/test/", "/TEST/" };
      char *sNewUrls[] = { "/go/", "/GO/" };

      for (int i=0; i<2; i++)
      {
            if (iUrl = strstr(pUrl,sOldUrls[i]))
            {
                  doSet = TRUE;
                  memcpy(iUrl,sNewUrls[i],strlen(sNewUrls[i]));
            }
      }

      if (doSet) pHeaders->SetHeader(pfc, "url", pUrl);
}

DWORD WINAPI HttpFilterProc(HTTP_FILTER_CONTEXT *pfc,DWORD NotificationType,VOID * pvData)
{
      PHTTP_FILTER_PREPROC_HEADERS pHeaders;
      DWORD cUrlOrig = MAX_URL_LEN;
      DWORD cUrl = cUrlOrig;
      CHAR rgUrl[MAX_URL_LEN];
      CHAR *pUrl;
      BOOL result;

      switch ( NotificationType )
      {
            case SF_NOTIFY_PREPROC_HEADERS:
                  pHeaders = (PHTTP_FILTER_PREPROC_HEADERS) pvData;
                  result = pHeaders->GetHeader(pfc, "url", rgUrl, &cUrl);

                  if (!result && cUrl > cUrlOrig)
                  {
                        pUrl = (CHAR*)LocalAlloc(0, cUrl);
                        result = pHeaders->GetHeader(pfc, "url", pUrl, &cUrl);
                        if (!result)
                        {
                              LocalFree(pUrl);
                              break;
                        }
                        ReMapURLs(pUrl, pfc, pHeaders);
                        LocalFree(pUrl);
                  }
                  else
                        ReMapURLs(rgUrl, pfc, pHeaders);
                  break;
            default:
                  break;
      }
      return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

Rewrite Apache logs that have incorrect dates

// Rewrite Apache logs that have incorrect dates

#!/usr/bin/perl

# $Id$

# Rewrite Apache logs that have incorrect dates.
# Example usage: $0 '28/May/2006:01:17:14 +0200' '19/Jan/2007:08:49:14 +0100' \
#                access_log.* error_log.*

use strict;
use warnings;

## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
our ($VERSION) = '$Revision$' =~ m{ \$Revision: \s+ (\S+) }xms;
## use critic

use HTTP::Date;

my @DAYS   = qw(Sun Mon Tue Wed Thu Fri Sat);
my @MONTHS = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

my $wrong_datetime = shift @ARGV;
my $right_datetime = shift @ARGV;

my ($timezone) = $right_datetime =~ m/ ([+-]\d\d\d\d)\z/xms;

my $seconds_diff = str2time($right_datetime) - str2time($wrong_datetime);

foreach my $file (@ARGV) {
    print "Rewriting $file\n";
    open my $IN,  '<', $file
        or die "Can't open $file: $!\n";
    open my $OUT, '>', "$file.rewritten"
        or die "Can't write to $file.rewritten: $!\n";
    while (<$IN>) {
        if (m{
               \A
               (.+\s+) # Before date and time (if any)
               \[
               (
                   \d\d/\w\w\w/\d\d\d\d # Date
                   :\d\d:\d\d:\d\d      # Time
                   \s
                   [\+\-]\d\d\d\d       # Time zone
               )
               \]
               (\s+.+) # After date and time
               \z
             }xms) {
            print {$OUT} 
              $1, q{[},
              rewrite_access_datetime($2, $seconds_diff, $timezone),
              q{]}, $3;
        }
        elsif (m{
               \A
               \[
               (
                   \w\w\w \s \w\w\w \s \d\d \s # Date
                   \d\d:\d\d:\d\d \s           # Time
                   \d\d\d\d                    # Year
               )
               \]
               (\s+.+) # After date and time
               \z
             }xms) {
            print {$OUT} 
              q{[},
              rewrite_error_datetime($1, $seconds_diff),
              q{]}, $2;
        }
        else {
            print {$OUT} $_;
        }
    }
}

sub rewrite_access_datetime {
    my ($datetime, $seconds_diff, $timezone) = @_;
    
    my ($sign, $hours, $minutes) = $timezone =~ m/\A([+-])(\d\d)(\d\d)\z/xms;
    my $seconds_offset = ($hours * 60 + $minutes) * 60;
    
    $datetime = str2time($datetime) + $seconds_diff;
    if    ($sign eq q{+}) {
        $datetime = $datetime + $seconds_offset;
    }
    elsif ($sign eq q{-}) {
        $datetime = $datetime - $seconds_offset;
    }
    
    my ($sec, $min, $hour, $mday, $mon, $year) = gmtime $datetime;
    return sprintf '%02d/%s/%04d:%02d:%02d:%02d %s',
        $mday, $MONTHS[$mon], $year + 1900, $hour, $min, $sec, $timezone;
}

sub rewrite_error_datetime {
    my ($datetime, $seconds_diff) = @_;
    
    $datetime = str2time($datetime) + $seconds_diff;
    
    my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime $datetime;
    return sprintf '%s %s %02d %02d:%02d:%02d %04d',
        $DAYS[$wday], $MONTHS[$mon], $mday, $hour, $min, $sec, $year + 1900;
}

Redirect all HTTP requests to HTTPS with ISAPI Rewrite

To redirect all HTTP requests to HTTPS with ISAPI Rewrite you can use the following rewriting rules in your httpd.ini file:

# redirect all http requests  to https
RewriteCond  %HTTPS (?!on).*
RewriteCond Host: (.*)
RewriteRule (.*) https\://$1$2 [I,RP]


Thanks to Ivan Uzunov

replace file extension with apache rewrite

// in this example *.html is replaced by *.php
// its a redirect [R] and it is is permanent (code is 301)

RewriteEngine On
RewriteBase   /the_directory

RewriteRule  ^(.*).html$ $1.php [R=301]

rewrite rules

// if cms has cryptic urls
// in apache url-rewriting with .htaccess can help

RewriteRule cms/fest$ cms/index.php?option=content&task=view&id=176&Itemid=202 [NC]


// [NC] = not case-sensitive
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS