admin管理员组文章数量:1321433
Wordpress has a feature whereby it will automatically redirect your URLs if it percieves them to be written wrongly. Here is an example: I have a page called my-page
If I go to:
www.mysite/something/my-page/
it will immediately redirect me to
www.mysite/my-page/
as nothing exists at the first URL.
How can I turn this feature off, and instead just get a 404 if incorrect URL's are typed in?
Wordpress has a feature whereby it will automatically redirect your URLs if it percieves them to be written wrongly. Here is an example: I have a page called my-page
If I go to:
www.mysite/something/my-page/
it will immediately redirect me to
www.mysite/my-page/
as nothing exists at the first URL.
How can I turn this feature off, and instead just get a 404 if incorrect URL's are typed in?
Share Improve this question edited Sep 24, 2020 at 18:22 Jesse Nickles 7357 silver badges19 bronze badges asked Nov 8, 2012 at 11:13 MazatecMazatec 1,0262 gold badges13 silver badges20 bronze badges 3- If you watch what is happening (via HttpFox or WireShark or a similar packet sniffer), those pages redirect with a 301 Status-- Moved Permanently-- which is appropriate behavior. It helps search engines keep their databases up to date among other things, and the redirect helps users find pages. This may effect how WordPress handles things when you actually do move a page or change a permalink, so be careful. Anyway, your choice but I am not sure it is a good idea. – s_ha_dum Commented Nov 8, 2012 at 15:04
- I know it's a good thing, I do understand that. However, I have a particular use case where I did not need that behaviour. I am developing a plugin which handles URL redirection and so I want to override that behavior. – Mazatec Commented Nov 9, 2012 at 11:28
- Here is a more correct answer: link – Binod Kalathil Commented Dec 19, 2014 at 10:54
3 Answers
Reset to default 33This worked for me:
remove_action('template_redirect', 'redirect_canonical');
As Ash suggested, you can turn off the feature by using the following code:
remove_action('template_redirect', 'redirect_canonical');
In looking at the redirect_canonical function in canonical.php, it would appear you can also modify the behavior with your own filter.
At the end of the redirect_canonical() function, there is a call to filter the final answer:
$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
So you could write your own filter to modify the final redirection or return null to stop the redirection, based upon the input, thus turning off the feature for a particular URL or a subset of URLs.
You can disable permalink guessing for 404s without disabling redirection of canonical URLs by adding the following line somewhere in your code (eg. in functions.php
):
add_filter('do_redirect_guess_404_permalink', '__return_false');
Relevant functions in the Wordpress code are redirect_canonical
and redirect_guess_404_permalink
in wp-includes/canonical.php
.
本文标签: redirectHow to prevent automatic redirection of 404 errors and quotincorrectquot URLs
版权声明:本文标题:redirect - How to prevent automatic redirection of 404 errors and "incorrect" URLs? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742102251a2420854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论