Sreedev S B
Sreedev S B

Reputation: 279

Remove duplicate content issue using htaccess

My .htaccess file looks likes this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?pg=$1 [L]

When a non-file or non-dir url is requested, it works fine.

But if index.php?pg=(pagename) is requested, it also works which can possibly lead to duplicate content issue.

Is there a way to prevent this?

Upvotes: 1

Views: 473

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19016

You're right, this can lead to duplicate content.
And, yes, it is possible to avoid it.

This solution redirects old url to new url equivalent, for instance http://website.com/index.php?pg=get/my/page/please.html to http://website.com/get/my/page/please.html.

RewriteEngine On
RewriteBase /

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

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

Upvotes: 1

Related Questions