Search code examples
.htaccesshttp-redirectidx

Redirect Old IDX home listing pages to Search Page


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

Solution

  • 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' );