WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make a force redirect for single.php to index so that it is never reachable by direct access (the site is built in a way that you retrieve all the content on the index page, still need to have this page available though because of an ajax function). I assume this is done in .htaccess?

Edit: I need to be able to access the single.php through an ajax request from the index page, i.e. the redirect should only occur upon direct url access.

share|improve this question
You can delete single.php file and index.php will be used instead, or that isn't options in your case? – Mamaduka Sep 16 '12 at 14:46
No, I still need the single page. Otherwise I would have solved it long ago. – Staffan Estberg Sep 16 '12 at 14:56
up vote 1 down vote accepted

put this code in functions.php file

add_action('wp', 'myfun');
 function myfun(){ 
            wp_redirect( home_url() ); exit;  


hope this trick solve your problem. all the best ;)

share|improve this answer
Awesome, thanks! :) Plus points for not having to dig into WP's dreaded .htaccess... – Staffan Estberg Sep 16 '12 at 15:04
Ok, so maybe I got a little bit too excited. If I try and fetch the single page now via an ajax load from the index page, I simply return index itself. It seems it redirects to index either way you choose to access it. How can this be avoided? – Staffan Estberg Sep 16 '12 at 15:18
try this... pass a parameter with ajax request url. here I am using "mode" parameter for example... add_action('wp', 'myfun'); function myfun(){ $mode = $_REQUEST['mode']; if(is_single() && !$mode == 'ajax'){ wp_redirect( home_url() ); exit; } } – Lalit Kaushik Sep 16 '12 at 15:50
That did it! Just realised I need to maintain the URL from the direct access though but I think I can solve that on my own. Thanks! – Staffan Estberg Sep 16 '12 at 16:06

You could also try,

add_action('wp', 'myfun');
function myfun(){ 

    if($_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest' && is_single()){

       //ajax function here

    } elseif (if($_SERVER['HTTP_X_REQUESTED_WITH']=='' && is_single())) {

            wp_redirect( home_url() ); exit;  



Or here is a JavaScript based solution from Ben Heller on this very topic,

if (window.top.location == window.location) {
   window.location.replace("<?php echo bloginfo('siteurl');?>#work");

Or as suggested by techguy4web you can pass a custom parameter and check for its existence much in the same fashion.

Some supporting material:

WordPress plugins:

Ajax Content Renderer

Sometimes you just need to retrieve the body of a post or page without the header, footer and their includes. This plugin detects Ajax requests and returns just the formatted body of the post or page.

Infinite Scroll

Automatically append the next page of posts (via AJAX) to your page when a user scrolls to the bottom.

share|improve this answer
Thanks! I decided to go along with techguy4web's solution. – Staffan Estberg Sep 16 '12 at 16:11
No problem, probably a more reliable option since headers can be spoofed, though I've provided some additional information anyway as to other alternative solutions should anyone else find your question. – userabuser Sep 16 '12 at 16:15

You can use tamplate_include filter and force WordPress to retrieve index.php instead of single.php.

function mamaduka_template_include( $template ) {
    if ( is_single() )
        $template = get_index_template();

    return $template;
add_filter( 'template_include', 'mamaduka_template_include' );
share|improve this answer

Your Answer


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.