首页 > 解决方案 > 将旧 IDX 主页列表页面重定向到搜索页面

问题描述

在房屋售出并且列表下线后,我们在房地产网站上收到很多 404 错误。我正在尝试通过 htaccess 将 Google Search Console 显示为 404 的缺失页面重定向到主页搜索页面。我已经尝试了下面的代码,但它正在重定向所有列表页面,而不仅仅是那些不再存在的页面。不确定这是我的代码还是因为页面是动态创建的。

所有的房屋清单都在 www.example.com/homes-for-sale-details/[address] 下。如果列表不再存在,我希望页面重定向到 www.example.com/homes-for-sale-details。

我的 htaccess 代码

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

我哪里错了?

非常感谢!

编辑(添加更多 htaccess 代码):

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

标签: .htaccessredirectidx

解决方案


我试图让它与 .htaccess 文件一起使用,但不能。上面脚本的问题是您没有首先检查 404 错误,因此来自该“文件夹”的所有请求都被重定向。我试过在 .htaccess 文件中使用 ErrorDocument 指令,但没有成功,所以我想出的解决方案是只使用 wordpress 已经提供的内容。在您的主题子文件夹 (/wp-content/themes/THEMENAME) 中应该有一个 404.php 文件。如果它不存在,您可以创建它。您可以使用此文件顶部的一些代码来解析请求 url 并重定向页面。像这样的东西应该工作:

<?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 } ?>

只要记住备份您当前的 404.php 页面,以防您覆盖了您无意覆盖的内容。如果您想尝试使用 htaccess 中的 ErrorDocument 内容进行此操作,请成为我的客人,但无论出于何种原因,它对我不起作用。这是一篇展示如何做到这一点的帖子

祝你好运!


推荐阅读