Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

Use flush_rewrite_rules():

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

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.

share|improve this answer
add comment

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.

share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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