How to avoid duplicate content mapping urls with htaccess

最后都变了- 提交于 2019-12-11 20:00:39

问题


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!


回答1:


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]


来源:https://stackoverflow.com/questions/26247557/how-to-avoid-duplicate-content-mapping-urls-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!