【问题标题】:Redirect Old IDX home listing pages to Search Page将旧 IDX 主页列表页面重定向到搜索页面
【发布时间】:2018-05-08 18:53:09
【问题描述】:

在房屋售出且房源下线后,我们在房地产网站上收到大量 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

【问题讨论】:

  • 当您访问这些页面时,您确定您的网站返回的是实际的 404 吗?
  • 另外,请阅读this answer
  • 这是 Search Console 报告的内容:“Googlebot 无法抓取此网址,因为它指向一个不存在的页面。通常,404 不会损害您网站的搜索性能,但您可以使用它们有助于改善用户体验。”访问页面时会显示 WordPress 404 页面
  • “homes-for-sale-details”是您网络服务器上的物理目录吗?
  • 还有,这个 .htaccess 文件在哪里?

标签: .htaccess redirect idx


【解决方案1】:

我试图让它与 .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 内容进行此操作,请成为我的客人,但无论出于何种原因,它对我不起作用。 Here's a post that shows how to do that.

祝你好运!

【讨论】:

  • 谢谢苗条。我真的很感谢你在这方面的帮助。我将研究选项并报告我前进的方向。再次感谢!!
  • 嘿,我们应该创建一个 wordpress 插件来做到这一点!如果您愿意,请告诉我。
  • 查看您的代码后,我偶然发现了一个类似的解决方案,其中包含一些我可以使用 template_redirect 重定向挂钩放入 functions.php 的代码。将发布我使用的代码。
【解决方案2】:

在尝试找到 htaccess 答案失败后,slim 的答案让我有所收获,我找到了这篇帖子 WordPress Template Redirect(非常感谢您对 slim 的帮助!!!)

这是我对该代码所做的修改。

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

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    相关资源
    最近更新 更多