5

I need to be able to browse to http://www.example.com/index.php, but WordPress automatically 301 redirects this to http://www.example.com/.

Is it possible to stop this redirection ONLY for the homepage?

Here is my .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
Shane
  • 1,190
  • 15
  • 28
  • Wordpress is actually doing the right thing. Allowing any content to be reached with different URIs is considered duplicate content. Even worse if its the landing page. –  Apr 10 '15 at 17:00

2 Answers2

5

The redirection occurs in the redirect_canonical function. There is a filter applied to the redirect URL before the redirection occurs:

$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );

If you hook into that filter you should be able to disable the redirection.

add_filter('redirect_canonical', function($redirect_url, $requested_url) {
    if($requested_url == home_url('index.php')) {
        return '';
    }
}, 10, 2);

I verified that this is working by adding the above filter to my theme's functions.php file. Note that this filter must be attached before the redirect action fires so placing the filter in a template file will not work.

Mathew Tinsley
  • 6,805
  • 2
  • 27
  • 37
  • I tried testing, but it doesn't even look like it's hitting my filter. I wonder if the homepage doesn't go through that, or I'm simply doing something wrong. – Shane Apr 09 '15 at 21:48
  • I verified that this works by adding that filter to my theme's `functions.php` file. Where are you adding that filter? – Mathew Tinsley Apr 09 '15 at 21:51
  • Silly mistake on my part. This works great. Thank you. – Shane Apr 10 '15 at 00:30
0

This should work. Add under RewriteBase /.

DirectoryIndex <somerandomfile>.php
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://www\.example\.com/index\.php$1 [R=301,L]
Krii
  • 907
  • 9
  • 23
  • I'm getting a redirect loop when I try to browse using the above code, it also makes the url http://www.example.com/index.phpindex.phpindex.php... – Shane Apr 09 '15 at 21:47
  • Did you remove the code originally under the `RewriteBase /`? The page is looping because your directory index is "index.php" so the webpage is just redirecting to the same page. You need to change the directory index for this to work. – Krii Apr 09 '15 at 21:53