.htaccess permalink rewrite problem

by 9 replies
11
I need some help with an .htaccess rewrite.


I have a site that had this permalink: /archive/%post_id
and changed it to this: /%postname%/


I have tried every single permalink redirect and change plugin - none of them work for this scenario.


The site has 15,000 posts, adding one line 301 redirects in the .htaccess file isn't going to work.


Can someone help me add a rewrite that will redirect all instances of /archive/%post_id to /%postname%/ ?


any help appreciated!
#programming #htaccess #permalink #problem #rewrite
  • Since you are working with two seperate variables I don't think a simple redirect would work.

    The old method uses an id number and the new method uses a postname. There htaccess file has no way of matching those two up.

    What you might have to do is create a php file that queries the database and finds the postname for the id number of the new file and then redirect from there.

    The htaccess would redirect any links to the old url to the new php file. That file will find the new address and redirect to it.

    I might be wrong but this is the only way I know to handle it since htacess has no idea where to send the user since it can't find the new url.
    • [1] reply
    • AHA - so that would explain why none of the plugins for permalink redirects work! Ok, got it, thx for the help!


      Kind Regards,

      - John
      • [1] reply
  • I have encountered that problem as well then i visited w3 school and got solution..
    • [1] reply
  • Permalink Migrator didn't work?
    • [1] reply
    • no current plugin on the market will work, it's mixing apples and oranges to redirect an ID to a pretty permalink.
  • You're going to need to be a little creative here...

    To preface all this, I need to say that using a plugin is not the way to go - for any redirects, IMHO. The reason is that before any redirects can even be sent back to the browser, at least a dozen WP files have to load and execute. Unneeded load on your server.

    This isn't just a job for a rewrite. You're going to have to intercept the old URLs for processing while leaving the current URLs alone.

    You're going to need to write a short script to do the actual redirection.

    Here's the way I would do it, I think
    1) check the URL to see if it matches the old permalink structure
    2) if it does, pass the processing off to the script
    3) if it doesn't, let it go through to WP

    You'll need to make sure you leave the existing WP rewrite rules intact.

    NOTE: this is untested, you may (probably will) need to tweak it a little.
    Code:
    # BEGIN new rules
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^/archives/.*$
    RewriteRule . /redirect_script.php [L]
    </IfModule>
    # END new rules
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    So the first section looks for URLs with '/archive/' and sends them to your redirect script. If it doesn't match that, it passes through to the original WP rewrite rules.

    Your redirect script will have to strip the post id from the URL, then look up the post_title from the wp_posts table. Then do a header('Location: ') redirect to the new URL.

Next Topics on Trending Feed