简体   繁体   中英

How to redirect all subdomain urls to main domain

For some reason search engines are indexing my addon domains on my hosting. They should not do that.

For example I just found urls like

addondomain/maindomain.com

how to prevent this happening? How did search engines even find my addondomains?

What is the solution here? I tried this

RewriteEngine on
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com
RewriteRule ^(.*)$ http://www\.maindomain\.com [L]

but when I visit the url for example

addondomain/maindomain.com for example nothing happens?

Try to Change in .httaccess file. You can replace your rule with this rule::

RewriteCond %{HTTP_HOST} =shop.domain.abc
RewriteRule ^ http://www.domain.abc/? [R=301,L]

Using %{REQUEST_URI} will cause original URI to be copied in target. Trailing ? in target will strip off any pre-existing query string.

This answer is a further explanation to my comment on your question.

how to prevent this happening? How did search engines even find my addondomains?

Google can easily find these subdomains on your site. To prevent this from happening, you can set a redirection with a 301 status code to inform Google that it should not index the addon domain. By doing this, Google will update its index as well.

This is a very common scenario with shared hosting and specially when you use CPanel. In Hostgator's support pages, you can see they have mentioned about this behavior.

Addon URL Example

For the primary domain abc.com, if you assign the addon domain 123.com to the folder "123," the following URLs would be correct:

 abc.com/123 123.abc.com 123.com 

All three of these paths would access the same directory and show the same website. For visitors going to 123.com, there is no evidence that they are being routed through 123.abc.com.

https://support.hostgator.com/articles/cpanel/what-is-an-addon-domain

You can fix this by adding the following to your .httaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]

NC - match in a case-insensitive manner.

R - causes a HTTP redirect to be issued to the browser. When given as R=301 it will be issued with a 301 status code which is required to inform Google that their index should be updated accordingly.

L - Causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.

===========================================================

Edit: Updated to add redirection to all domains, as requested in the comments.

To do this, you can simply check if the hostname is equal to your main domain, and if it's not, redirect it to the main domain.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM