【问题标题】:Codeigniter duplicate content with mod_rewrite使用 mod_rewrite Codeigniter 重复内容
【发布时间】:2014-03-05 10:41:57
【问题描述】:

在 codeigniter 中,一旦启用短网址,您就有可能重复内容,因为尽管您的新网址看起来像:

http://domain.com/privacy_policy

您仍然可以手动访问旧链接,这些链接在您输入时仍然会加载:

http://domain.com/index.php/privacy_policy

根据手册,我的 htaccess 文件如下所示:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

我应该怎么做才能解决这个问题?

【问题讨论】:

  • 您必须从 url 中永久删除 index.php,这样页面就无法使用 index.php 访问。检查此链接以了解如何从 codeigniter stackoverflow.com/questions/14783666/… 中的 url 中删除 index.php
  • @RahulChipad 不知道你在说什么。即使您从 url 中删除 index.php 页面,由于RewriteCond $1 !^(index\.php)%{REQUEST_FILENAME} !-f 规则,您仍然可以访问原始地址,如 OP 所述。请检查我的答案。

标签: .htaccess codeigniter duplicate-content


【解决方案1】:

您可以通过将用户代理重定向到新 URL 来解决此问题。

方法 #1:使用 htaccess

RewriteBase / 之后使用以下内容作为第一个 RewriteRule:

RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index.php(?:/(.*))?$ $1 [R=301,L]

如果您没有使用过RewriteBase,您可能需要将$1 更改为http://example.com/$1

其他规则必须在上述规则之后。

方法#2:用PHP处理

我建议extend the CI_Controller如下:

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        // Execute CI_Controller Constructor
        parent::__construct();

        // Get the index page filename from config.php
        // For perior to PHP 5.3 use the old syntax.
        $index = index_page() ?: 'index.php';

        // Whether the 'index.php' exists in the URI
        if (FALSE !== strpos($this->input->server('REQUEST_URI', TRUE), $index))
        {
            // Redirect to the new address
            // Use 301 for permanent redirection (useful for search engines)
            redirect($this->uri->uri_string(), 'location'/*, 301*/);
        }
    }
}

但是,搜索引擎不会索引看起来像 index.php/privacy_policy 的 URL,除非您在页面中使用过这样的 URL 地址。

此外,您可以在页面中使用 canonical link element 到页面的make search engines index only one version 以获取搜索结果:

<link rel="canonical" href="http://domain.com/privacy_policy">

尤其是在 CodeIgniter 中:

<link rel="canonical" href="<?php echo base_url(uri_string()); ?>">

【讨论】:

    【解决方案2】:

    config/config.php 上,在$config['index_page'] 上删除index.php

    $config['index_page'] = '';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-27
      • 2018-01-20
      • 2012-12-09
      • 2014-11-09
      • 1970-01-01
      • 2012-11-12
      • 2012-05-15
      • 1970-01-01
      相关资源
      最近更新 更多