I currently have my links like this /%postname%/ -
I'd like to change them to this /%post_id%/%postname%/ (because I read that it helps the site load faster).
Is there way to have /%postname%/ links (on other sites) automatically redirect to the /%post_id%/%postname%/ links?
Thanks in advance,
nando
Not really, since that's not a 'standard' format that can be grep'd or regex'd easily.
Problem: Postname can be ANYTHING so while we can search for (.*), we would have to have a way to KNOW what Post ID == Postname, and that you can't really do outside of WP.
If you have a small number of posts you could manually do 301 redirects in your .htaccess (I did that for a site with 30 posts - not TOO horrible).
i have way to many posts to do that lol
thanks tho... i thought there would be something i can put in the header that would just append the id to the url and redirect... i thought i read something like that some where...
You know, scrap all that.
I use domain.com/YYYY/POSTNAME and I just went to domain.com/POSTNAME and it redirected me... So maybe WordPress is bloody brilliant! Try it.
just tried it, did NOT work... i was hoping tho lol
wow theres has to be a way, hopefully lol
You could add some conditional code to your theme's header, but it might result in a few redirect loops.
That conditional code would look something like the code posted at http://pastebin.com/vhQ3SghQ I think (I haven't tested that code, and am working from memory at the moment).
You would have to put that either at the very top of your 404.php template file (above the call to get_header() or at the very top of your header.php file; as it uses the PHP header() function, which will generate an error if any code or text is sent to the browser beforehand.
hey, thanks!!
i put the code above get_header{} in my 404 template but unfortunately when i go to post that should give me a 404 (and then redirect) i get a blank white page....
any additional help would be greatly appreciated since i'm pretty noob at this...
Curtiss, that is amazing! AWESOME!
That means there's a PHP error (probably something simple in the code I provided you). Do you have access to your error logs? If so, can you reload the page that causes the blank white screen, then check the logs and paste in the most recent errors?
isetenu, u got it to work?
heres the last minute of errors (a lot)
[Tue May 17 15:01:20 2011] [info] [client 168.10.192.3] (32)Broken pipe: core_ou
tput_filter: writing data to the network
[Tue May 17 15:01:20 2011] [error] [client 168.10.192.3] PHP Fatal error: Call
[Tue May 17 15:01:20 2011] [info] [client 168.10.192.3] (32)Broken pipe: core_ou
tput_filter: writing data to the network
utput_filter: writing data to the network
[Tue May 17 15:01:16 2011] [info] [client 64.56.91.45] (32)Broken pipe: core_out
put_filter: writing data to the network
[Tue May 17 15:01:16 2011] [error] [client 64.56.91.45] PHP Fatal error: Call t
o a member function get() on a non-object in /usr/local/apache228/htdocs/wp-incl
udes/cache.php on line 93
[Tue May 17 15:01:16 2011] [info] [client 64.56.91.45] (32)Broken pipe: core_out
put_filter: writing data to the network
[Tue May 17 15:01:16 2011] [info] [client 64.56.91.45] (32)Broken pipe: core_out
put_filter: writing data to the network
[Tue May 17 15:01:16 2011] [error] [client 64.56.91.45] PHP Fatal error: Call t
o a member function get() on a non-object in /usr/local/apache228/htdocs/wp-incl
udes/cache.php on line 93
[Tue May 17 15:01:17 2011] [info] [client 64.56.91.45] (32)Broken pipe: core_out
put_filter: writing data to the network
[Tue May 17 15:01:19 2011] [info] [client 168.10.192.3] (32)Broken pipe: core_ou
tput_filter: writing data to the network
[Tue May 17 15:01:19 2011] [error] [client 168.10.192.3] PHP Fatal error: Call
to a member function get() on a non-object in /usr/local/apache228/htdocs/wp-inc
ludes/cache.php on line 93
[Tue May 17 15:01:19 2011] [info] [client 168.10.192.3] (32)Broken pipe: core_ou
tput_filter: writing data to the network
[Tue May 17 15:01:20 2011] [info] [client 168.10.192.3] (32)Broken pipe: core_ou
tput_filter: writing data to the network
[Tue May 17 15:01:20 2011] [error] [client 168.10.192.3] PHP Fatal error: Call
to a member function get() on a non-object in /usr/local/apache228/htdocs/wp-inc
ludes/cache.php on line 93
@nando99 - My guess is that you pasted the <?php and ?> tags inside of an area that's already wrapped in <?php and ?> tags.
Having two sets of <?php (opening) tags without a set of ?> (closing) tags in between will throw an error. I just tested it briefly on one of my sites and I didn't get any PHP errors, so that issue is my best guess right now.
Try removing the <?php and ?> tags from the code I posted on Pastebin and see if that fixes the issue.
If not, try restoring your 404.php file to its original format, then copy and paste the whole block of code (including the opening and closing PHP tags) at the very top of your 404.php file (before the opening <?php tag in that file).
You could wrap it in is_404() and put it in your header.php file too.
http://codex.wordpress.org/Function_Reference/is_404
this is how i have it:
[Code moderated as per the Forum Rules. Please use the pastebin]
Oooh. Just noticed what is probably causing the issue. The original code I provided was trying to feed an array of post objects into the get_permalink() function. Try the code at http://pastebin.com/ku8y1LgA and see if that fixes the issue.
I'm pretty sure I LOVE YOU right now... thanks!!!!!!!!!!!!!!!!!!!!!!!
actually... it doesnt work... like, it redirects but to a specific post every time.... post with id 17729... thats extremely weird...
actually... all 404s are redirecting to that post...
That means that something must be wrong in the get_posts() arguments I provided. I'll try to take a look at that later to see what might be going on with it.
cool thanks curtiss...
isstenu, i was, but i disabled the plugin
thanks again, for the help!
Let's try this again. I was using the wrong parameters in the get_posts() function (post_name should have been name and posts_per_page should have been numberposts), so WordPress was ignoring both of those parameters and just running a general query for posts.
Here is some updated code that appears to do what it should http://pastebin.com/5jD9nxLj
Let me know if this works for you.
nope... now 404s redirect to another random post...
That's really strange. I'll have to set up a test site with parameters similar to what you have set up and see if I can figure out what the issue is.
yeah... thanks for your help, really appreciate it...
There's something funky going on. I just set up a new test environment and set it up with the same permalink settings you indicated above, and it worked out of the box without me having to add the 404 code I'd been working on.
If you visit http://wp-testing.ten-321.com/another-post-with-everything-in-it/ you'll see that it automatically redirects to http://wp-testing.ten-321.com/56/another-post-with-everything-in-it/
I am using the standard TwentyTen theme with no plugins or modifications on that site.
I wonder if something in either your theme or one of your plugins is stopping that functionality from working properly.
i'm using version 2.9.1... and i'm also using that remove category from the url plugin... thoughts?
Odds would be the remove cat plugin. But also you're two revs back, man. You should upgrade if you can.
yeah, i really cant upgrade right now... extremely customized and i dont have the time to start from scratch on the newest version...
as soon as i get a chance i'm going to install a new instance of it with no plugins and see if it works or now..