.htaccesscodeigniterduplicate-content

Codeigniter duplicate content with mod_rewrite


In codeigniter, once you enable short urls, you have the possibility of duplicate content because although your new URLs look like:

http://domain.com/privacy_policy

You can still manually access the old links, which still load when you type in:

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

My htaccess file, per the manual, looks like:

RewriteEngine On
RewriteBase /

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

What should I do to solve this problem?


Solution

  • You could resolve this issue by redirecting the user agents to a new URL.

    Method #1: Using htaccess

    Use the following just after RewriteBase /, as the first RewriteRule:

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

    You might want to change $1 to http://example.com/$1 if you haven't used RewriteBase.

    The other rules MUST come after the above rule.

    Method #2: Handle with PHP

    I'd suggest to extend the CI_Controller as follows:

    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*/);
            }
        }
    }
    

    However, the search engines won't index the URLs which look like index.php/privacy_policy, Unless you've used such URL addresses in your page.

    Also, you could use a canonical link element within your pages to make search engines index only one version of the page for their search results:

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

    Particularly in CodeIgniter:

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