WordPress.org

Ready to get started?Download WordPress

Forums

Problem between .htaccess 404s and 301s, and permalink structure? (2 posts)

  1. StephenChapman
    Member
    Posted 1 year ago #

    Hi! I'm a bit perplexed by an issue I'm having. First, let's start with my .htaccess file contents:

    `# 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>

    Options All -Indexes

    # END WordPress`

    Now, that "Options All -Indexes" line keeps things so that all of my open directories are blocked. That all works beautifully; however, I see 404s for directories/non-existing pages. Wanting to redirect all 404s to my home page, I modified my .htaccess to the following:

    `# 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,R=301]
    </IfModule>

    Options All -Indexes

    # END WordPress`

    The change I made was this: RewriteRule . /index.php [L,R=301]

    That works great, except that it also redirects all of my actual posts to the main page; not just the 404s. I'm thinking it's an issue with my permalink structure, which is this:

    /%postname%/%post_id%

    I tried adding a / to the end of that, but it didn't make a difference. I'm sure the solution is a simple one, but it's not making itself apparent to me after hours of searching and trial and error. Thanks to anyone with some advice!

  2. StephenChapman
    Member
    Posted 1 year ago #

    Well, I managed to get working what I was looking to do. To block access to all of my open indexes (which, in turn, makes them show 404s), then 301 redirect all of those 404s to my home page, I changed 2 things: .htaccess and 404.php.

    My .htaccess looks like this:

    # 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>
    
    Options All -Indexes
    
    # END WordPress

    And my 404.php has this code in it (and nothing more):

    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ".get_bloginfo('url'));
    exit();
    ?>

    Hopefully that helps someone out there! If anyone sees any issues with my method above, feel free to reply with other resolutions. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic