0

I'm having some hard-time with with WordPress permalinks. WordPress 3.1 seems to force url redirection, ex:

This is the correct post url:

http://www.mysite.com/test/1/mypost.html

If I go to any of the url's below WordPress is redirecting me to the url above

http://www.mysite.com/test/1/mypost-teste21.html
or
http://www.mysite.com/test/1/blabla-mypost-example99.html

Basically, what I want to achieve is that WordPress matches the post id, in this case 1, and presents the post content without redirecting users to the correct url. Any ideas on how to achieve this ?

Thank you all!

PS- Please don't move this question to SO WordPress because I've already tried to get an answer there for a couple of days without luck.

9
  • Can copy/paste your .htaccess?
    – Whiler
    Sep 5 '11 at 11:32
  • default .httacess for wordpress permalinks - pastebin.com/3EZSTD9p Sep 5 '11 at 11:36
  • It's not done by .htaccess -- such correction is done by PHP code. It is there to ensure link integrity/consistency (important for SEO purposes). For example -- take this question URL, remove some text from it (or alter it somehow) and submit -- it will redirect to this URL again. Do not know about you .. but this is correct approach in general.
    – LazyOne
    Sep 5 '11 at 11:37
  • @lazyOne- I'm also sure that this won't be accomplished by .htaccess, but I also don't know what php code to change in order to achieve what I need. Sep 5 '11 at 11:39
  • Try post a question there and you'll understand why I've posted here - I've deleted the other question. Sep 5 '11 at 11:41
1

First, I'll say it's beyond me why you'd want to do this, I really don't think it's a good idea, but in any case...

If you look in /wp-includes/canonical.php, you'll see how WP is fighting you on this. The only way I'm aware of to get around it is to go looking for the post yourself based on the post id query var (which should be set and hopefully correct) and resetting the $wp_query global to whatever is found, then manually setting the status header so a 404 isn't sent to the browser.

function wpse27871_404_fakeout() {
    global $wp_query;
    if( $wp_query->is_404 && get_query_var('p') ):
        $this_query = new WP_Query( 'p='.get_query_var('p') );
        if( $this_query->post_count !== 0 ):
            $wp_query = $this_query;
            status_header(200);
        endif;
    endif;
}
add_action('wp', 'wpse27871_404_fakeout');
1
  • Milo : your solution works fine, the post is presented to the user even if the url is incomplete, although, the page misses the meta tags keyword and description, that are blank, any idea on how to solve this? thanks! Sep 9 '11 at 8:51
6

Since you deleted your previous question here, perhaps somebody already suggested this to you and it doesn't work, but you might try:

remove_filter('template_redirect', 'redirect_canonical');

2
  • Doesn't work for my purposes, gives me a 404, thanks anyway. Sep 8 '11 at 12:59
  • For me worked great because it was a 404 I was looking for:) Thank you:)
    – viery365
    Nov 4 '16 at 14:38
0

Try after disabling the SEO plugins like Yoast SEO. They usually redirect to nearest possible matched pages instead of throwing it to 404.

-1

This answer doesn't work!

I don't remove it to avoid similar wrong answer. I'll delete it if a good one is post. In your .htaccess, you probably have some RewriteRule which finish with: [L].

To keep the URL, your have to use the proxy parameter: [P,L]

Regarding the default .htaccess you provided Tuga, I'll try like this:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [P,L]

Like this, the URL are internaly translated (in Apache), and it stays the same in the browser. But, within WordPress, there's maybe other redirections which can affect the URL itself :/

2
  • Do you mind to exemplify the "use the proxy parameter", thanks. Sep 5 '11 at 11:42
  • I appreciate your help but it didn't work, still redirecting. Sep 5 '11 at 20:40

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.