Redirection help about .htaccess file

I am trying to redirect the URLs starting with /blog/201 and ending with .php. I tried adding these lines to .htaccess but it does not help:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/blog\/201.*\.php$ /blog/ [R=301,L]
</IfModule>

  • James Morris
    • WordPress Enthusiast

    Hello Stuart

    Would you please try adding the following to the very beginning of your .htaccess file?

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^blog/201(.*)\.php$ /blog/? [L,R=301]
    </IfModule>

    Please note: You may need to clear your browser cache if you've been testing other redirects before. Let us know if this works for you. :slight_smile:

    Best regards,

    James Morris

  • Stuart
    • Site Builder, Child of Zeus

    Hi James,

    I placed the rewrite code you sent at the top of my .htaccess file, which is located in the /blog directory where WP lives. I cleared the browser cache, too. Unfortunately, we did not get the result we hoped. It appears the .htaccess is being ignored. Is there a way to trace the effect of the code in the .htaccess file? My client is on a shared Bluehost server but has a unique IP address.

    Here is the weird URL Google cooked up and on which I clicked:
    https://moldbusters.com/blog/2017/01/25/prevent-flooding/residential-services/residential-services/air-duct-cleaning.php

    Here is the URL that appeared in the Chrome browser:
    https://moldbusters.com/blog/2017/01/25/prevent-flooding/residential-services/residential-services/air-duct-cleaning.php

    They are identical, so it didn't work. It also looks like the / right after blog in your regex string was not escaped. I added a \ before it, but that made no difference.

    Here is a clip of the .htaccess file contents as you had specified it.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^blog/201(.*)\.php$ /blog/? [L,R=301]
    </IfModule>
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /blog/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]
    </IfModule>

    # END WordPress

  • James Morris
    • WordPress Enthusiast

    Hello Stuart

    I placed the rewrite code you sent at the top of my .htaccess file, which is located in the /blog directory where WP lives.

    Ahhhh!!!! This makes a big difference! My rules were to match a subdirectory of blog. The entire /blog/ portion can be removed.

    The URL queries you gave does not match the notes I saw in the ticket, but your additional information helps a great deal!

    It also looks like the / right after blog in your regex string was not escaped.

    It doesn't need to be because it's actually outside the regex. The only regex in my condition is (.*)

    I've tested this on my site with the corrected condition. You can see a live example here (temporarily).

    http://ss.wpdevtests.com/blog/2017/01/25/prevent-flooding/residential-services/residential-services/air-duct-cleaning.php

    This redirects to http://ss.wpdevtests.com/blog/

    Here's the corrected .htaccess code that *should* work.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^201(.*)\.php$ /blog/? [L,R=301]
    </IfModule>

    Let us know if you have any further questions. We'll be happy to help! :slight_smile:

    Best regards,

    James Morris

  • Stuart
    • Site Builder, Child of Zeus

    Hey James,

    After replicating your correct model, here is the final set of rewrites that seem to fix all of Google's strange 404 URLS. Thanks much for your help with this!

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^201(.*)\.php$ /blog/? [L,R=301]
    RewriteRule ^201(.*)\-services\/$ /blog/? [L,R=301]
    RewriteRule ^cat(.*)\.php$ /blog/? [L,R=301]
    RewriteRule ^cat(.*)\/blog\/$ /blog/? [L,R=301]
    RewriteRule ^tag(.*)\.php$ /blog/? [L,R=301]
    RewriteRule ^tag(.*)\/blog\/$ /blog/? [L,R=301]
    RewriteRule ^aut(.*)\.php$ /blog/? [L,R=301]
    RewriteRule ^aut(.*)\/blog\/$ /blog/? [L,R=301]
    </IfModule>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.