0

We are getting a lot of 404 errors on a Real Estate website after houses are sold and the listing goes offline. I am trying, via htaccess, to redirect the missing pages that Google Search Console shows as 404 to a home listing search page. I have tried the code below but it is redirecting all listing pages not just the ones that no longer exist. Not sure if it's my code or because the pages are dynamically created.

All the home listings are under www.example.com/homes-for-sale-details/[address]. If the listing no longer exists I want the page to redirect to www.example.com/homes-for-sale-details.

My htaccess code

# Redirect old home listing to a search page
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
</IfModule>

Where are I going wrong?

Many Thanks!

Edit (added more of htaccess code):

####################################
# START Redirect pages from old site
#
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^Properties https://www.example.com/home-listings [L,NC,NE,R=301]
    RewriteRule ^Access https://www.example.com/search-homes [L,NC,NE,R=301]
    RewriteRule ^Neighboorhoods https://www.example.com/neighborhoods [L,NC,NE,R=301]
    RewriteRule ^Buyer-Resources https://www.example.com/buy-home-in-colorado-springs [L,NC,NE,R=301]
    RewriteRule ^Relocation-Guide https://www.example.com/buy-home-in-colorado-springs/relocation-guide [L,NC,NE,R=301]
    RewriteRule ^Buyer-Resources/Buyer-Finance/Finance-Information https://www.example.com/buy-home-in-colorado-springs/home-finance [L,NC,NE,R=301]
    RewriteRule ^Seller-Resources https://www.example.com/sell-colorado-springs-home [L,NC,NE,R=301]
    RewriteRule ^Area-Schools https://www.example.com/local-lifestyle/area-schools [L,NC,NE,R=301]
    RewriteRule ^Colorado-Springs-Attractions https://www.example.com/local-lifestyle/colorado-springs-attractions [L,NC,NE,R=301]
    RewriteRule ^Military-Bases https://www.example.com/local-lifestyle/military-bases [L,NC,NE,R=301]
    RewriteRule ^About$ https://www.example.com/about-us [L,NC,NE,R=301]
    RewriteRule ^contact$ https://www.example.com/contact-us [L,NC,NE,R=301]
    RewriteRule ^Terms-Of-Service https://www.example.com/terms-of-service [L,NE,R=301]
    RewriteRule ^Privacy-Policy https://www.example.com/privacy-policy [L,NE,R=301]
    RewriteRule ^Site-Map https://www.example.com/sitemap [L,NC,NE,R=301]

    RewriteRule ^neighborhoods/fountain$ https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
    RewriteRule ^neighborhoods/securitywidefield https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
    RewriteRule ^park-avenue-properties-blog https://www.example.com/blog [L,NC,NE,R=301]
    RewriteRule ^Primary-Factors-the-Affect-the-Real-Estate-Market https://www.example.com/primary-factors-affect-real-estate-market [L,NC,NE,R=301]
</IfModule>
# END Redirect pages from old site

# Force HTTPS
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80 
    RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
    RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
    RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>

# Remove "Blog" from blog post URLs and preserve blog paging
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !page
    RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
    RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
    RewriteRule ^Blog/(.*)$ /$1 [L,NC,R=301]
</IfModule>

# Redirect old home listing to a search page
#<IfModule mod_rewrite.c>
#    RewriteEngine On
#    RewriteBase /
#    RewriteCond %{REQUEST_FILENAME} !-d
#    RewriteCond %{REQUEST_FILENAME} !-f
#    RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
#</IfModule>

####################################
# Browser caching code removed :)
####################################

# 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
ZeroGravity
  • 79
  • 10
  • Are you sure your website is returning an actual 404 when you visit those pages? – slim May 09 '18 at 13:18
  • Also, read [this answer](https://stackoverflow.com/a/41580649/1823954) – slim May 09 '18 at 13:20
  • This is what Search Console reports: "Googlebot couldn't crawl this URL because it points to a non-existent page. Generally, 404s don't harm your site's performance in search, but you can use them to help improve the user experience." When accessing the pages the WordPress 404 page is displayed – ZeroGravity May 10 '18 at 18:08
  • is "homes-for-sale-details" a physical directory on your web server? – slim May 11 '18 at 16:29
  • also, where is this .htaccess file located? – slim May 11 '18 at 16:35
  • .htaccess is in the root directory. It is a WordPress site so "homes-for-sale-details" isn't a physical directory. I have created a page "Homes For Sale Details." All the home listings use this page as the parent page. – ZeroGravity May 14 '18 at 20:01
  • WP Page: "www.example.com/homes-for-sale-details" example home listing: "www.example.com/homes-for-sale-details/123-some-drive-somewhere-co-98765/mls-number/68" - The home listing is automatically generated by the IDX – ZeroGravity May 14 '18 at 20:13
  • I'm sorry for the questions, but are you sure you're using apache as your webserver? I'm setting up a test environment and I need to know this. Also, wordpress typically uses .htaccess itself to redirect all requests through their index.php file. Take a look at this post, it describes the process for turning on the debug log for mod rewrite. You should be able to see the problem in the log (or at least know if your htaccess file is even consulted): https://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite – slim May 15 '18 at 13:28
  • No Worries with the questions. I really appreciate your help. It's on a shared hosting account so I don't think I have the necessary access to turn the logging on. :( The server information in cPanel says - Apache Version 2.4.33. I do believe it is using LiteSpeed but not sure where I can find the version number. I had a look under WHM as well but can't see anything there. – ZeroGravity May 15 '18 at 17:58
  • As far as I know, wordpress will overwrite the .htaccess file if permalinks are setup in your website. Can you verify that the .htaccess file looks like the one you have above and what permalinks are set to? (settings -> permalinks) – slim May 15 '18 at 19:11
  • I'm fairly certain that wordpress depends on the .htaccess file to redirect requests to their index.php page. If you change it, you may find that your website no longer works past the home page. – slim May 15 '18 at 19:30
  • I have other directives in the htaccess redirecting pages from the previous site to relevant pages on the new one, forcing HTTPS etc. Also for browser caching etc. These are working. I have added a more detailed example of my htaccess code to my post. – ZeroGravity May 15 '18 at 20:18

2 Answers2

1

I've tried to get this to work with the .htaccess file and could not. The issue with your script above is that you're not checking first for the 404 error, so all requests from that "folder" are being redirected. I've tried using the ErrorDocument directive in the .htaccess file with no luck, so the solution I worked out was to just use what wordpress already provides. In your themes subfolder (/wp-content/themes/THEMENAME) there should be a 404.php file. If it's not there you can create it. You can use some code at the top of this file to parse the request url and redirect the page. Something like this should work:

<?php 
    if (strpos($_SERVER['REQUEST_URI'], 'homes-for-sale-details') !== false)
         header('Location: /homes-for-sale-details', true, 301);
    else {
?>
    // ... paste the current 404.php content here
<?php } ?>

Just remember to back up your current 404.php page in case you overwrite something you didn't mean to. If you want to try to get this working with the ErrorDocument stuff in htaccess be my guest, but for whatever reason, it wasn't working for me. Here's a post that shows how to do that.

Good luck!

slim
  • 2,545
  • 1
  • 24
  • 38
  • Thanks slim. I really appreciate your help with this. I will look into the options and report back on the direction I go. Thanks Again!! – ZeroGravity May 16 '18 at 20:46
  • Hey, we should create a wordpress plugin that does this! Let me know if you want to. – slim May 17 '18 at 13:34
  • After looking at your code I stumbled across a similar solution with some code that I could put in the functions.php using the template_redirect redirect hook. Shall post the code I used. – ZeroGravity May 18 '18 at 17:15
0

After unsuccessfully trying to find a htaccess answer slim's answer got me thinging and I found this post WordPress Template Redirect (thank you so much for your help slim!!!)

Here is the modification I made to that code.

// Redirect missing home listing to a search page.
function __404_template_redirect()
{
    if( is_404() )
    {
        $req = $_SERVER['REQUEST_URI'];

        if ( is_file( $req )) {
            return; // don't reduce perf by redirecting files
        }

        // check if "homes-for-sale-details" is in the URL
        if ( strpos($req, 'homes-for-sale-details') == false ) {
            return; // only redirect missing homes
        }

        // pull the parent directory and convert to site url
        $parent_url = get_permalink( 1232 );

        // redirect to parent directory
        wp_redirect( $parent_url, 301 );
        exit();
    }
}
add_action( 'template_redirect', '__404_template_redirect' );
ZeroGravity
  • 79
  • 10