Initial Commit

This commit is contained in:
2013-02-08 20:18:32 +00:00
commit eaaf97ef7e
61 changed files with 3375 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
<?php
class HtRule_Rule_AppendSlash extends HtRule
{
public static function GetName()
{
return 'Append /';
}
public function RequireRewriteEngine()
{
return True;
}
public function __toString()
{
$name = self::GetName();
return <<<EOT
# $name
RewriteCond %{REQUEST_URI} !^.*/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteRule (.*) $1/ [R=301,L]
EOT;
}
}

View File

@@ -0,0 +1,26 @@
<?php
class HtRule_Rule_Comment extends HtRule
{
public function __construct()
{
$this->AddParam(new HtRule_Param('text', 'Comment', HtRule_Param::TYPE_TEXT, 'A Comment'));
parent::__construct();
}
public static function GetName()
{
return 'Comment';
}
public function RequireRewriteEngine()
{
return False;
}
public function __toString()
{
$text = $this->params['text']->Value();
$text = '# '.str_replace("\n", "\n# ", $text);
return $text;
}
}

View File

@@ -0,0 +1,22 @@
<?php
class HtRule_Rule_Custom extends HtRule
{
public static function GetName()
{
return 'Custom Directives';
}
public function __construct()
{
$this->AddParam(new HtRule_Param('Text', 'Directives', HtRule_Param::TYPE_TEXT));
parent::__construct();
}
public function __toString()
{
$params = $this->ParamValues();
return $params['Text'];
}
}

View File

@@ -0,0 +1,38 @@
<?php
class HtRule_Rule_DenyFiles extends HtRule
{
public static function GetName()
{
return 'Deny access to File Types';
}
public function __construct()
{
$this->AddParam(new HtRule_Param('filetypes', 'File Type', HtRule_Param::TYPE_TEXT, ".sql\n.log", 'Specify the file extensions that you want to deny access to (one per line)', False));
parent::__construct();
}
public function __toString()
{
$params = $this->ParamValues();
$types = $params['filetypes'];
$types = str_replace("\r", "", $types);
$types = explode("\n", $types);
$rules = '';
for($i=0;$i < count($types);$i++)
{
$type = preg_quote($types[$i]);
$rules .= "RewriteCond %{REQUEST_FILENAME} $type$";
if ($i < count($types))
$rules .= ' [OR]';
$rules .= "\n";
}
$rules .= "RewriteRule .* - [F,L]\n";
return $rules;
}
}

View File

@@ -0,0 +1,31 @@
<?php
class HtRule_Rule_EnableRewrite extends HtRule
{
public static function GetName()
{
return 'Enable Rewrite Engine';
}
public function RequireRewriteEngine()
{
return True;
}
public function __construct()
{
$this->AddParam(new HtRule_Param('base', 'Rewrite Base', HtRule_Param::TYPE_STRING, '/', 'With leading and trailing slashes'));
parent::__construct();
}
public function __toString()
{
$base = $this->params['base']->Value();
$name = self::GetName();
return <<<EOT
# $name
RewriteEngine On
RewriteBase $base
EOT;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class HtRule_Rule_FromWWW extends HtRule
{
public static function GetName()
{
return 'Redirect from www.';
}
public function RequireRewriteEngine()
{
return True;
}
public function __construct()
{
$this->AddParam(new HtRule_Param('domain', 'Domain Name', HtRule_Param::TYPE_STRING, 'domain.com', 'The domain without the www'));
$this->AddParam(new HtRule_Param('https', 'Use Https', HtRule_Param::TYPE_BOOLEAN, False, 'Redirect to https'));
parent::__construct();
}
public function __toString()
{
$domain = $this->params['domain']->Value();
$name = self::GetName();
$proto = 'http';
if ($this->params['https']->Value())
$proto = 'https';
return <<<EOT
# $name
RewriteCond %{HTTP_HOST} ^www.${domain}$
RewriteRule (.*) ${proto}://${domain}/$1 [R=301,L]
EOT;
}
}

View File

@@ -0,0 +1,33 @@
<?php
class HtRule_Rule_IndexToRoot extends HtRule
{
public static function GetName()
{
return 'Redirect index.php to /';
}
public function RequireRewriteEngine()
{
return True;
}
public function __construct()
{
$this->AddParam(new HtRule_Param('indexfile', 'Index File', HtRule_Param::TYPE_STRING, 'index.php'));
parent::__construct();
}
public function __toString()
{
$name = self::GetName();
$file = str_replace('.', '\\.', $this->params['indexfile']->Value());
return <<<EOT
# $name
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteRule ^$file$ / [R=301,L]
EOT;
}
}

View File

@@ -0,0 +1,39 @@
<?php
class HtRule_Rule_MapIndex extends HtRule
{
public static function GetName()
{
return 'Map urls to index.php';
}
public function RequireRewriteEngine()
{
return True;
}
public function __construct()
{
$this->AddParam(new HtRule_Param('Target', 'Target File', HtRule_Param::TYPE_STRING, 'index.php', 'The file to map request to'));
$this->AddParam(new HtRule_Param('AppendUrl', 'Append Url to rewrite', HtRule_Param::TYPE_BOOLEAN, False, 'This adds the url to the end of the rewritten url, ie /test/ => /index.php/test/'));
$this->AddParam(new HtRule_Param('AppendMethod', 'Method of appending', HtRule_Param::TYPE_STRING, '/', 'eg. ? or ?page= (/ May not work in a CGI environment)'));
parent::__construct();
}
public function __toString()
{
$params = $this->ParamValues();
$append = '';
if ($params['AppendUrl'])
$append .= $params['AppendMethod'].'$1';
return <<<EOT
# Map urls to $params[Target]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) $params[Target]{$append} [L,QSA]
EOT;
}
}

View File

@@ -0,0 +1,45 @@
<?php
class HtRule_Rule_RedirectDomain extends HtRule
{
public function __construct()
{
$this->AddParam(new HtRule_Param('permanent', 'Permanent', HtRule_Param::TYPE_BOOLEAN, true, ''));
$this->AddParam(new HtRule_Param('subdomains', 'Include All Subdomains', HtRule_Param::TYPE_BOOLEAN, true, ''));
$this->AddParam(new HtRule_Param('to', 'Destination Domain', HtRule_Param::TYPE_STRING, 'http://newdomain.com/', 'The full url to direct to'));
$this->AddParam(new HtRule_Param('from', 'Source Domains', HtRule_Param::TYPE_TEXT, 'olddomain.com', ''));
parent::__construct();
}
public static function GetName()
{
return 'Redirect Domains';
}
public function __toString()
{
$values = $this->ParamValues();
$type = ($values['permanent']) ? 301 : 302;
$typeText = ($values['permanent']) ? 'Permanent' : 'Temporary';
$domains = explode("\n", str_replace("\r", '', $values['from']));
$subdomains = ($values['subdomains']) ? '' : '^';
$rules = "# Redirect Domains ($typeText)".PHP_EOL;
if (empty($domains))
return $rules;
foreach($domains as $domain)
{
$rules .= "RewriteCond %{HTTP_HOST} {$subdomains}".preg_quote($domain)."$ [OR]".PHP_EOL;
}
$rules = substr($rules, 0, strlen($rules)-strlen(" [OR]".PHP_EOL)).PHP_EOL;
$destination = rtrim($values['to'], '/');
$destination = preg_quote($destination);
$rules .= "RewriteRule ^(.*)$ {$destination}/$1 [R=$type,L]";
return $rules;
}
}

View File

@@ -0,0 +1,96 @@
<?php
class HtRule_Rule_RedirectUrls extends HtRule
{
public function __construct()
{
$this->AddParam(new HtRule_Param('permanent', 'Permanent Redirects', HtRule_Param::TYPE_BOOLEAN, True, ''));
$this->AddParam(new HtRule_Param('host', 'Check Host', HtRule_Param::TYPE_BOOLEAN, false, 'Check the hostname is correct for each redirect'));
$this->AddParam(new HtRule_Param('ignorewww', 'Ignore www. prefix', HtRule_Param::TYPE_BOOLEAN, false, 'Ignores the www, from the domain name. Requires check host be enabled'));
$this->AddParam(new HtRule_Param('urls', 'Urls', HtRule_Param::TYPE_DUALTEXT, '', 'Full Urls, from on the left, to on the right', True));
parent::__construct();
}
public static function GetName()
{
return 'Redirect Urls';
}
public function __toString()
{
$values = $this->ParamValues();
$type = ($values['permanent']) ? 301 : 302;
$typeText = ($values['permanent']) ? 'Permanent' : 'Temporary';
$urls = $values['urls'];
$rules = '';
if (is_array($urls) && count($urls) == 2)
{
$fromUrls = explode("\n", str_replace("\r", '', $urls[0]));
$toUrls = explode("\n", str_replace("\r", '', $urls[1]));
foreach($fromUrls as $key => $from)
{
$from = trim($from);
if ($from == "")
continue;
if (isset($toUrls[$key]))
{
$to = trim($toUrls[$key]);
if ($to == "")
continue;
$rule = $this->CreateRewrite($from, $to, $type, $values['host'], @$values['ignorewww']);
if ($rule)
$rules .= $rule.PHP_EOL;
}
}
}
return <<<EOT
# Redirect Urls ($typeText)
$rules
EOT;
}
private function CreateRewrite($from, $to, $type, $checkhost, $ignorewww)
{
$from = parse_url($from);
$toUrl = $to;
$to = parse_url($to);
if (!$from || !$to)
return False;
$default = array('scheme'=>false,'host'=>false,'port'=>false,'path'=>false,'query'=>false,'fragment'=>false);
$from = array_replace($default, $from);
$to = array_replace($default, $to);
$rule = '';
if ($checkhost)
{
$domain = $from['host'];
$domainCheck = '^'.str_replace(array('.','\-'),array('\.','\-'),$domain).'$';
if ($ignorewww)
{
$domain = preg_replace('/^www\./', '', $domain);
$domainCheck = "^(www\.)?".str_replace(array('.','\-'),array('\.','\-'),$domain)."\$";
}
$rule .= 'RewriteCond %{HTTP_HOST} '.$domainCheck.PHP_EOL;
}
if (!empty($from['query']))
{
$end = '';
if ($to['query'] == false)
$end = '?';
$rule .= "RewriteCond %{QUERY_STRING} ^".str_replace('.','\.',$from['query'])."\$".PHP_EOL;
$rule .= "RewriteRule ^".ltrim(preg_quote($from['path']),'/')."\$ $toUrl$end [R=$type,L]";
}
else
{
$rule .= "RewriteRule ^".ltrim(preg_quote($from['path']),'/')."\$ $toUrl [R=$type,L]";
}
return $rule;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class HtRule_Rule_ToWWW extends HtRule
{
public static function GetName()
{
return 'Redirect to www.';
}
public function RequireRewriteEngine()
{
return True;
}
public function __construct()
{
$this->AddParam(new HtRule_Param('domain', 'Domain Name', HtRule_Param::TYPE_STRING, 'domain.com', 'The domain without the www'));
$this->AddParam(new HtRule_Param('https', 'Use Https', HtRule_Param::TYPE_BOOLEAN, False, 'Redirect to https'));
parent::__construct();
}
public function __toString()
{
$domain = $this->params['domain']->Value();
$name = self::GetName();
$proto = 'http';
if ($this->params['https']->Value())
$proto = 'https';
return <<<EOT
# $name
RewriteCond %{HTTP_HOST} ^${domain}$
RewriteRule (.*) ${proto}://www.${domain}/$1 [R=301,L]
EOT;
}
}

View File

@@ -0,0 +1,48 @@
<?php
class HtRule_Rule_UseHttps extends HtRule
{
public static function GetName()
{
return 'Force SSL';
}
public function RequireRewriteEngine()
{
return True;
}
public function __construct()
{
$this->AddParam(new HtRule_Param('DetectHttps', 'Detect Using %{HTTPS}', HtRule_Param::TYPE_BOOLEAN, True, 'This will work on most servers'));
$this->AddParam(new HtRule_Param('Domain', 'Rewrite To Domain', HtRule_Param::TYPE_STRING, 'www.domain.com', 'The full domain to rewrite to'));
$this->AddParam(new HtRule_Param('DetectPort', 'Detect By Port Number', HtRule_Param::TYPE_BOOLEAN, False, 'HTTP uses port 80 whereas HTTPS uses port 443 (default ports)'));
$this->AddParam(new HtRule_Param('HttpsPort', 'Https Port', HtRule_Param::TYPE_INT, 443, 'Redirect the request if the port in use is not this port'));
parent::__construct();
}
public function __toString()
{
$params = $this->ParamValues();
$rule = "# Force clients to use https\n";
if ($params['DetectPort'])
{
$rule .= "RewriteCond %{SERVER_PORT} !$params[HttpsPort]";
if ($params['DetectHttps'])
$rule .= " [OR]";
$rule .= "\n";
}
if ($params['DetectHttps'])
$rule .= "RewriteCond %{HTTPS} !=on\n";
$port = '';
if ($params['HttpsPort'] != 443)
$port = ':'.$params['HttpsPort'];
$rule .= "RewriteRule (.*) https://$params[Domain]{$port}/$1 [R=301,L]";
return $rule;
}
}