forked from donatj/RewriteRule-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
128 lines (110 loc) · 3.96 KB
/
index.php
File metadata and controls
128 lines (110 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* RewriteRule Generator
*
* @license MIT
* @author Jesse G. Donat <donatj@gmail.com> http://donatstudios.com
*
*/
error_reporting(E_ALL ^ E_NOTICE);
$output = '';
/**
* @param string $from
* @param string $to
* @param bool $show_comments
* @return string
*/
function generateApacheRewrite( $from, $to, $show_comments ) {
$parsedFrom = parse_url(trim($from));
$parsedTo = parse_url(trim($to));
$line_output = "";
if( $show_comments ) {
$line_output .= PHP_EOL . '# ' . $_POST['type'] . ' --- ' . $from . ' => ' . $to . PHP_EOL;
}
if( $parsedFrom['host'] != $parsedTo['host'] ) {
$line_output .= 'RewriteCond %{HTTP_HOST} ^' . quotemeta($parsedFrom['host']) . '$';
$line_output .= PHP_EOL;
$prefix = $parsedTo['scheme'] . '://' . $parsedTo['host'] . '/';
} else {
$prefix = '/';
}
$explodedQuery = explode('&', $parsedFrom['query']);
foreach( $explodedQuery as $qs ) {
if( strlen($qs) > 0 ) {
$line_output .= 'RewriteCond %{QUERY_STRING} (^|&)' . quotemeta($qs) . '($|&)';
$line_output .= PHP_EOL;
}
}
$line_output .= 'RewriteRule ^' . quotemeta(ltrim($parsedFrom['path'], '/')) . '$ ' . $prefix . ltrim($parsedTo['path'], '/') . '?' . $parsedTo['query'] . ($_POST['type'] == 'Rewrite' ? '&%{QUERY_STRING}' : ' [L,R=301]');
$line_output .= PHP_EOL;
return $line_output;
}
if( $_POST['tabbed_rewrites'] ) {
$_POST['tabbed_rewrites'] = preg_replace('/(\t| )+/', ' ', $_POST['tabbed_rewrites']); // Spacing Cleanup
$lines = explode(PHP_EOL, $_POST['tabbed_rewrites']);
if( strlen(trim($_POST['tabbed_rewrites'])) ) {
foreach( $lines as $line ) {
$line = trim($line);
if( $line == '' ) continue;
$explodedLine = explode(" ", $line);
if( count($explodedLine) != 2 ) {
$output .= PHP_EOL . '# MALFORMED LINE SKIPPED: ' . $line . PHP_EOL;
continue;
}
$line_output = generateApacheRewrite($explodedLine[0], $explodedLine[1], (bool)$_POST['desc_comments']);
$output .= $line_output;
}
}
} else {
$_POST['desc_comments'] = 1;
$_POST['tabbed_rewrites'] = <<<EOD
http://www.test.com/test.html http://www.test.com/spiders.html
http://www.test.com/faq.html?faq=13&layout=bob http://www.test2.com/faqs.html
text/faq.html?faq=20 helpdesk/kb.php
EOD;
}
?>
<script src="//ajax.googleapis.com/ajax/libs/mootools/1.2.2/mootools-yui-compressed.js"></script>
<script>
window.addEvent('domready', function() {
var insertAtCursor = function( myField, myValue ) {
//IE support
if( document.selection ) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if( myField.selectionStart || myField.selectionStart == '0' ) {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
myField.selectionEnd = myField.selectionStart = startPos + myValue.length;
} else {
myField.value += myValue;
}
};
var input = $('tsv-input');
var output = $('rewrite-output');
input.addEvent('keydown', function( e ) {
if( e.key == 'tab' ) {
e.stop();
insertAtCursor(e.target, "\t");
}
});
output.addEvent('click', function( e ) {
e.target.select();
});
});
</script>
<form method="post">
<textarea id="tsv-input" cols="100" rows="20" name="tabbed_rewrites" style="width: 100%; height: 265px;"><?php echo htmlentities($_POST['tabbed_rewrites']) ?></textarea><br />
<select name="type">
<option>301</option>
<option<?php echo $_POST['type'] == 'Rewrite' ? ' selected="selected"' : '' ?>>Rewrite</option>
</select>
<label><input type="checkbox" name="desc_comments" value="1"<?php echo $_POST['desc_comments'] ? ' checked="checked"' : '' ?>>Comments</label>
<br />
<textarea id="rewrite-output" cols="100" rows="20" readonly="readonly" style="width: 100%; height: 265px;"><?php echo htmlentities($output) ?></textarea><br />
<center><input type="submit" /></center>
</form>