Here's the scenario.

Assume I have a site, wordpress.site, which has the following Permalink structure: /%postname%

Assume I then set up the following posts:

  • /post1
  • /post2
  • /post3

If someone were to type in "wordpress.site/post1/" (extra slash), Wordpress will automatically 301 redirect the user to "wordpress.site/post1".

So far so good.

But now, let's imagine that there's an old URL "/post4/", and for SEO reasons, I want to create a post for this URL and keep the trailing slash?

In other words:

  • Calling wordpress.site/post4/ does not redirect to wordpress.site/post4 but
  • Calling wordpress.site/post4, would redirect to wordpress.site/post4/ and
  • Calling wordpress.site/post1 does not redirect, but
  • Calling wordpress.site/post1/ would redirect to wordpress.site/post1

How would I go about achieving this?

I attempted to do it by using add_rewrite_rule(), but couldn't seem to achieve what I was after (I was still redirected, and yes, I did flush the rewrite rules). Sample code:

add_action( 'init', 'my_rewrite_func' );
function my_rewrite_func()
    add_rewrite_rule( 'post4/$', 'index.php?p=12', 'top' );

Any suggestions?


Use flush_rewrite_rules():

add_action( 'init', 'my_rewrite_func' );
function my_rewrite_func() {
    add_rewrite_rule( 'post4/$', 'index.php?p=12', 'top' );

and / or whichever rewrite rules you use, you could always use /? for optional trailing slashes or force the trailing slash if you would prefer for it to always be there.

| improve this answer | |

i did not used it, but i heard a lot about this plugin http://wordpress.org/extend/plugins/redirection/ which is precisely for that kind of situation. I you don't want to use a plugin, maybe a lot at the source code could lead you to a solution.

| improve this answer | |
  • Thanks for the suggestion, Simon, but this definitely doesn't do what I'm after. I think perhaps I haven't explained the question properly, I'll edit it to try and clarify. – Craig Sefton Dec 12 '11 at 13:00
  • Ok, so i'll try again ^^ Maybe you could use .htaccess and write some redirections rules ? If it's SEO, using redirect permanent could do the trick. – Simon Dec 12 '11 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.