.htaccess permalink rewrite problem

9 replies
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!
#htaccess #permalink #problem #rewrite
  • Profile picture of the author Johnny Slater
    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.
    Signature

    {{ DiscussionBoard.errors[3446504].message }}
    • Profile picture of the author jtprattmedia
      AHA - so that would explain why none of the plugins for permalink redirects work! Ok, got it, thx for the help!


      Kind Regards,

      - John
      {{ DiscussionBoard.errors[3446539].message }}
      • Profile picture of the author Bruce Hearder
        As you most likely know, there is 2 ways to access a wordpress post.

        1. is via the permailinks eg : yourdomain.com/your-post-title-here

        2. Via a ?p=123 url, where 123 is the unique id of your post.

        Evey post has a unique id, so you could redirect using something like :

        RewriteRule archive\/(.+)$ www\.yourdomain\.com\?p=$1 [L]

        I haven't tested this, but something along these lines should work..

        Hope this helps

        Bruce
        {{ DiscussionBoard.errors[3451251].message }}
        • Profile picture of the author jtprattmedia
          Originally Posted by Bruce Hearder View Post

          As you most likely know, there is 2 ways to access a wordpress post.

          1. is via the permailinks eg : yourdomain.com/your-post-title-here

          2. Via a ?p=123 url, where 123 is the unique id of your post.

          Evey post has a unique id, so you could redirect using something like :

          RewriteRule archive/(.+)$ www.yourdomain.com?p=$1 [L]

          I haven't tested this, but something along these lines should work..

          Hope this helps

          Bruce
          Bruce,

          thanks for the comment. That would just redirect the old links to a valid page that displays the post - it wouldn't redirect the old post to the new permalink structure.
          {{ DiscussionBoard.errors[3453603].message }}
  • Profile picture of the author healthtourism
    I have encountered that problem as well then i visited w3 school and got solution..
    {{ DiscussionBoard.errors[3451254].message }}
    • Profile picture of the author jtprattmedia
      Originally Posted by healthtourism View Post

      I have encountered that problem as well then i visited w3 school and got solution..
      and what solution would that be?
      {{ DiscussionBoard.errors[3453592].message }}
  • Profile picture of the author jminkler
    Originally Posted by jtprattmedia View Post

    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!
    Permalink Migrator didn't work?
    {{ DiscussionBoard.errors[3451593].message }}
    • Profile picture of the author jtprattmedia
      Originally Posted by jminkler View Post

      Permalink Migrator didn't work?
      no current plugin on the market will work, it's mixing apples and oranges to redirect an ID to a pretty permalink.
      {{ DiscussionBoard.errors[3453589].message }}
  • Profile picture of the author SteveJohnson
    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.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3456494].message }}

Trending Topics