tessguefen
4/19/2015 - 6:24 PM

Super Fantastic 301 Generator

Super Fantastic 301 Generator

<html>
    <head>
    <link href='//fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
    <title>Super Fantastic 301 Redirect Generator</title>
    <style>
        body {margin:0;font-family: 'Montserrat', sans-serif;font-size: .9rem; color:#333;}
        h1 {text-align: left; background: #446CB3; color: #fff; padding: .5rem 2rem; font-size: 1rem; font-weight: normal;position:fixed; top:0; width: 100%; margin:0;-webkit-touch-callout: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;}
        h3 {font-weight: normal; color: #000; padding: 0 2rem;font-size:1rem; margin:1rem 0 0 0;}
        #wrap {margin-top: 45px; padding: 0 1rem;}
        pre {margin:0;}
        .padding {padding:0 2rem;}
        form {
            padding: 2rem;
            border: 1px solid #ccc;
            display: inline-block;
            margin: 0 2rem;
        }
        form .row {margin-bottom: 10px; display:inline-block;}
        form label {display: block; font-weight: normal;}
        form input[type=text] {border:1px solid rgba(0,0,0,.2); padding: 10px 16px;}
        form input[type=submit] {background: #52B3D9; color: #fff; border: 0; padding: 10px 16px; font-weight: 700;}
        .custom-file-input {position:relative; padding: 10px 0 0 89px;}
        .custom-file-input::-webkit-file-upload-button {visibility: hidden;}
        .custom-file-input::before {
            content: 'Upload your CSV File';
            display: inline-block;
            padding: 10px 16px;
            outline: none;
            white-space: nowrap;
            -webkit-user-select: none;
            cursor: pointer;
            color: #fff;
            font-weight: 700;
            font-size: 10pt;
            position:absolute;
            left:0; top: 3px;
            background: #52B3D9;
        }
        .custom-file-input:active {outline:none;}
        table td {padding:3px;}
        table tr:nth-child(even) {background: #D2D7D3;}
    </style>
    </head>
    <body>
    <h1>Super Fantastic 301 Redirect Generator</h1>
    <div id="wrap">
    <?PHP
    function readCSV($csvFile){
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle) ) {
            $line_of_text[] = fgetcsv($file_handle, 0);
        }
        fclose($file_handle);
        return $line_of_text;
    }
    if (isset($_POST['submit']) && isset($_FILES['csv']) && isset($_POST['domain']) && $_POST['domain'] !== '' && $_FILES['csv']['name'] !== '' && strpos($_FILES['csv']['name'], '.csv') !== false) {
        $working_file = 'temp/redirects.csv';
        move_uploaded_file($_FILES['csv']['tmp_name'], $working_file);
        // Set path to CSV file;
        $redirects = readCSV($working_file);
        $base = trim($_POST['domain']);
        $removeBase = array('http://', 'https://', 'www.');
        echo '<pre>';
        echo '### Redirects from CSV generated on '. date('m-d-Y') .''.PHP_EOL;
        $counter = 0;
        foreach ($redirects as $r) {
            $counter++;
            $oldUrl = str_replace($removeBase, '', trim($r['0']));
            $old = str_replace($base, '', $oldUrl);
            $newUrl = str_replace($removeBase, '', trim($r['1']));
            $new = str_replace($base, '', $newUrl);
            if ($newUrl == $base . '/') {
                $new = str_replace($removeBase, '', trim($r['1']));
                $new = 'http://www.' . $new .'mm5/merchant.mvc';
            }
            if ($counter > 1 && $old !== '' && $new !== '') {
                if(strpos($old,'?') !== false) {
                    $parts = parse_url($old);
                    parse_str($parts['query'], $query);
                    if ($query['Screen'] == 'PROD') {
                        echo 'RewriteCond %{QUERY_STRING} Product_Code='. $query['Product_Code'].PHP_EOL;
                        echo 'RewriteRule (.*) ' . $new .'? [R=301]'.PHP_EOL;
                    } elseif ($query['Screen'] == 'CTGY') {
                        echo 'RewriteCond %{QUERY_STRING} Category_Code='. $query['Category_Code'].PHP_EOL;
                        echo 'RewriteRule (.*) ' . $new .'? [R=301]'.PHP_EOL;
                    } elseif ($query['Screen']) {
                        echo 'RewriteCond %{QUERY_STRING} Screen='. $query['Screen'].PHP_EOL;
                        echo 'RewriteRule (.*) ' . $new .'? [R=301]'.PHP_EOL;
                    } else {
                        echo '#########'.PHP_EOL;
                        echo '### ERROR WITH '.$old.' CAN NOT MAKE REDIRECT TO '.$new;
                        echo PHP_EOL.'#########'.PHP_EOL;
                    }
                } else {
                    echo 'RewriteCond %{REQUEST_URI} ^'. $old .'$ [NC]'.PHP_EOL;
                    echo 'RewriteRule ^(.*)$ '. $new .'? [R=301,L]'.PHP_EOL;
                }
            }
        }
    echo '</pre>';
    unlink($working_file);
    } else {
        if(isset($_POST['submit'])) {
            echo '<p class="padding" style="color:#e74c3c;">Uh oh! Please make sure you upload a CSV file and enter a domain name.</p>';
        }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
        <div class="row">
            <input type="file" name="csv" class="custom-file-input" />
        </div>
        <div class="row">
            <label>Domain name</label>
            <input type="text" name="domain" placeholder="miva.com" />
        </div>
        <div class="row">
            <input type="submit" name="submit" value="Boomshakalaka!"/>
        </div>
    </form>
<h3>Example CSV file structure</h3>
<p class="padding">This is how the CSV should be uploaded:</p>
    <pre class="padding">
        <table>
            <tr>
                <th>Old</th>
                <th>New</th>
            </tr>
            <tr>
                <td>http://www.miva.com/product/yellow-bowtie.html</td>
                <td>http://www.miva.com/product/bowtie.html</td>
            </tr>
            <tr>
                <td>http://www.miva.com/mm5/merchant.mvc?Screen=PROD&Product_Code=bows</td>
                <td>http://www.miva.com/product/bows.html</td>
            </tr>
            <tr>
                <td>http://www.miva.com/mm5/merchant.mvc?Screen=CTGY&Category_Code=ribbons</td>
                <td>http://www.miva.com/category/ribbons.html</td>
            </tr>
            <tr>
                <td>http://www.miva.com/mm5/merchant.mvc?Screen=ABUS</td>
                <td>http://www.miva.com/about-us.html</td>
            </tr>
        </table>
    </pre>
    <a href="redirects.csv" class="padding">Click here for an Example CSV to send</a><br />
    <a href="http://www.htaccesscheck.com/" class="padding">Htaccess Check -- This will show you if you have any syntax errors.</a><br />
    <p class="padding">(Domain name would be 'miva.com')</p>
    <br/>Example output:<br />
    <pre style="line-height:55%;">
    ### Redirects from CSV generated on 03-10-2015<br />
    RewriteCond %{REQUEST_URI} ^/product/yellow-bowtie.html$ [NC]<br />
    RewriteRule ^(.*)$ /product/bowtie.html? [R=301,L]<br />
    RewriteCond %{QUERY_STRING} Product_Code=bows<br />
    RewriteRule (.*) /product/bows.html? [R=301]<br />
    RewriteCond %{QUERY_STRING} Category_Code=ribbons<br />
    RewriteRule (.*) /category/ribbons.html? [R=301]<br />
    RewriteCond %{QUERY_STRING} Screen=ABUS<br />
    RewriteRule (.*) /about-us.html? [R=301]
    </pre><br />
    <p class="padding">Please double check the CSV for any strange URL structures. The generator will only catch it if there are query strings, and if it does not match CTGY, PROD or SCREEN. Example Error output:</p>
    <pre class="padding" style="line-height:55%;">
#########<br />
### ERROR WITH /n2.g?login=baschdon&url= CAN NOT MAKE REDIRECT TO http://www.photodon.com/mm5/merchant.mvc<br />
#########
    </pre>
    <?php
    }
    ?>
        </div>
    </body>
</html>
Create CSV generator for rewritemaps (mps)
/old-url/blah.html new-url/whatever.html
/old-url/haha.html mm5/merchant.mvc

- Check if Rewrite can be changed to remove the /