Support » Plugins » Hacks » Need some php/database gurus for a custom redirect script…

  • Resolved spencerp

    (@spencerp)


    Here’s what I need some help with currently, my blog’s old permalink structure was: blog.com/id/123 and now I’ve changed it to: blog.com/post-name. I’d hate to lose all incoming traffic to the old permalink structure and etc. Someone a few years ago helped me with this before, this post as reference.

    Here’s the php/database codes from before…It was setup to redirect all incoming hits from: blog.com/post-name.htm to blog.com/id/123. For the wp-newuri.php file. Uploaded to the *root* of your domain.

    <?php
    mysql_connect("localhost", "DATABASE-USERNAME", "DATABASE-PASSWORD") or die("connect failed");
    
    mysql_select_db("DATABASE-NAME") or die("can't get to the database");
    
    $result = mysql_query("SELECT ID FROM wp_posts WHERE post_name = '".addslashes($_GET['q'])."';");
    while($row = mysql_fetch_assoc($result)) {
    foreach($row as $var => $val) {
    $post = $val;
    }
    }
    mysql_free_result($result);
    
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: /id/".$post);
    
    ?>

    And the htaccess file information for was:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+).htm wp-newuri.php?q=$1 [QSA,L]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    I need help changing the database call codes to locate the post_id then at end for redirection to send the people to the blog.com/post-name… So all blog.com/id/123 goes to blog.com/post-title. And the correct lines changed for htaccess mod rewrites. Anyone willing to help? Thanks…

    Spencer

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter spencerp

    (@spencerp)

    *Slaps self* Nevermind. I found two plugins to do the jobs… Thanks for the help.. lol. 😛

    Thread Starter spencerp

    (@spencerp)

    After a little more searching (wow, I’m so out of the “loop” on the new wordpress features/functions haha)… I managed to find out how to do redirects via the htaccess file for the old tags and category permalink structure.. however a little stumped on the: id/post_id > /dddd/postname coding… Hope to figure it all out soon here! LOL!

    What I have so far..

    RedirectMatch 301 /id/(.*) /$1
    RedirectMatch 301 /id/category/(.*) /category/$1
    RedirectMatch 301 /id/tag/(.*) /tag/$1

    # BEGIN WordPress
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress

    So blog.com/id/category/the-cat > blog.com/category/the-cat WORKS
    blog.com/id/tag/the-tag > blog.com/tag/the-tag WORKS
    blog.com/id/post_id > blog.com/2011/post-name NOT WORKING (yet)

    Need to figure out the proper line code for this:
    RedirectMatch 301 /id/(.*) /$1

    Reference post: http://www.blogtrafficexchange.com/remove-dates-from-permalink/

    Thread Starter spencerp

    (@spencerp)

    I think i got it now.. added this to htaccess file as well.. seems to work lol..

    RedirectMatch 301 /id/(.*) [0-9]{4}/$1

    Now posts like: blog.com/id/1075 takes you to the right post: blog.com/2010/the-post-name

    Wow, took me long enough! LOL! But it’s SO much nicer and easier this way with wordpress though! xD

    Thread Starter spencerp

    (@spencerp)

    Hmm, well it was working…some thing must be conflicting… >_< I think it’s this one… RedirectMatch 301 /id/(.*) [0-9]{4}/$1

    I remove that from htaccess, all redirect related plugins are deactivated of course.. And the blog.com/id/category/the-cat goes to > blog.com/category/the-cat just fine. And same with tags…

    default/custom permalink is: year/%postname%

    gotta figure out how to redirect the old /id/post_id to > 2010/post-name via htaccess or some such.. ugh. anyone? 😛

    Thread Starter spencerp

    (@spencerp)

    I’m not sure what’s matter, and no one else knows either? LOL!… I tried two more things from someone else from another forum… But those didn’t work either!

    RedirectMatch 301 /id/([^/]*) [0-9]{4}/$1
    OR
    RedirectMatch 301 ^/id/([^/]*)$ [0-9]{4}/$1

    Also weird, when I add for the post redirects in htaccess file, the tags and category redirects stop working…. Same happens when I enable a plugin for the: /id/post_id to redirect to new permalinks… the cats and tags redirects stop working. i disable the plugin, then the tag n cat redirects work…

    Thread Starter spencerp

    (@spencerp)

    Thanks Scott Yang! You’re the man! :D…

    RedirectMatch 301 /id/([0-9]+)/? /?p=$1

    Works!

    RedirectMatch 301 /id/([0-9]+)/? /?p=$1
    RedirectMatch 301  /id/category/(.*) /category/$1
    RedirectMatch 301  /id/tag/(.*) /tag/$1

    No need for bulky scripts and or plugins.. yay lmao

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Need some php/database gurus for a custom redirect script…’ is closed to new replies.