apache.htaccesshttp-redirectseoduplicate-content

How to avoid duplicate content mapping urls with htaccess


I'm kind of new to writing htaccess rules. I want to make the urls in my website as simple as possible.

Let's say I want to "map" /home/ directory for root domain. So, http://www.domain.com would display content from /home/ directory without changing url.

Moreover, I don't want users / google to access to: http://www.domain.com/home/, so, if somebody goes to that URL, I want to redirect them back to my root domain: http://www.domain.com.

I'm seeing that google is indexing both directories with the same duplicate content. http://www.domain.com http://www.domain.com/home/

Is there anything I can do to stop this? I don't want to use robots.txt because i've heard it's a bad practice.

RewriteEngine on
RewriteBase /

# Redirect /home/ dir to 'www.domain.com'
#RewriteCond %{REQUEST_URI} ^/home/$ [NC]
#RewriteRule ^(.*)$ / [R=301,L]

# Show /home/ dir for 'www.domain.com'
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /home/$1 [L]

If I enable all the lines from this code, I get a redirect loop that never ends.

Also, I'm facing the same problem when trying to add "seo friendly" urls. I'm trying to map "http://www.domain.com/thread/thread-name/" to "http://forum.domain.com/thread-name/", and although it works fine, I'm still having 2 duplicate URLS showing the same content.

I would like to heard the best practice to avoid search engines from indexing duplicate content. Should I redirect it to 404? I don't want to use robots.txt to implement this because there're lots of files / directories combination, and it won't work for my purpose.

Thanks a lot!


Solution

  • Instead of this:

    # Redirect /home/ dir to 'www.domain.com'
    #RewriteCond %{REQUEST_URI} ^/home/$ [NC]
    #RewriteRule ^(.*)$ / [R=301,L]
    

    try:

    # Redirect /home/ dir to 'www.domain.com'
    RewriteCond %{THE_REQUEST} \ /+home/([^\?\ ]*)
    RewriteRule ^ /%1 [R=301,L]