Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Created January 8, 2013 19:26
Show Gist options
  • Save itsjavi/4487080 to your computer and use it in GitHub Desktop.
Save itsjavi/4487080 to your computer and use it in GitHub Desktop.
Generates .htaccess redirect codes for a list of links
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>htaccess redirect generator</title>
<style>
html,body{
font-size:14px;
color:#222;
margin:0;
padding:0;
font-family: Helvetica, Arial, sans-serif;
}
#app{
position: relative;
width:940px;
margin:40px auto;
}
input, textarea{
width:916px;
resize:none;
border:1px solid #888;
color: #222;
display: block;
margin: 0 0 5px 0;
padding:10px 12px;
font-size:14px;
}
textarea{
height:200px;
font-size:11px;
font-family: monospace;
}
button{
font-size:14px;
background: #ddd;
border:1px solid #222;
padding:10px 20px;
margin:0;
text-align: center;
cursor: pointer;
}
form{
margin:0 0 20px 0;
}
#app *{
position: relative;
}
#results{
padding:20px;
font-size: 12px;
color:#f5f5f5;
background: #222;
overflow: auto;
}
</style>
</head>
<body>
<div id="app">
<h1>.htaccess Redirect Generator</h1>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
<input name="domain" placeholder="RewriteCond Domain" /><br />
<input name="baseurl" type="url" placeholder="Origin Base URL" /><br />
<textarea name="links" placeholder="Link redirections (in the format: origin - SPACE or TAB - destination url)"></textarea>
<br />
<div style="text-align:right">
<button type="submit">GENERATE</button>
</div>
</form>
<?php
if (isset($_POST["domain"])) :
$dom = preg_replace("/^www./", "", strtolower($_POST["domain"]));
$links = explode("\n", $_POST["links"]);
$link_count = count($links);
$htaccess = "";
if ((count($links) > 0) and (!empty($links[0]))) {
$allothers = false;
foreach ($links as $link) {
if (empty($link)){
$link_count--;
continue;
}
$link = explode(" ", str_replace(array("\t", "\n", " "), " ",$link), 2);
if (count($link) == 2) {
$dest = trim($link[1]);
$slug = trim(str_replace(trim($_POST["baseurl"], "/ "), "", trim($link[0], " /")), " /");
if(empty($slug)){
$allothers = $dest;
continue;
}
$htaccess .= "RewriteRule ^({$slug})/?$ {$dest} [L,R=301]\n";
}
}
if($allothers!==false)
$htaccess .= "# all other urls...\nRewriteRule .* {$allothers} [L,R=301]\n";
$htaccess = "# {$dom} Redirections\nRewriteCond %{HTTP_HOST} !^(www\\.)?{$dom} [NC]\n" .
"RewriteRule .? - [S=" . $link_count . "]\n".$htaccess."\n\n";
}
?>
<pre id="results"><?php echo $htaccess; ?></pre>
<?php endif; ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment