问题
Let's say my domain name is website123.com and I have a subdomain of my.website123.com
I've deleted the "my" subdomain from the site and want to make sure that anyone that goes to any page with a my.website123.com URL is redirected to the main www.website123.com URL. The "my" subdomain has a ton of pages of it, so I need to make sure that regardless of which page a user goes to on the "my" subdomain, that they're redirected to the site's main index page.
I was thinking to get this accomplished through .htaccess - is that the best way? If yes how?
回答1:
@joeh0717
Yes, for an .htaccess file modify it to this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} subdomain.domain.com
RewriteRule /(.*) http://www.domain.com [R=301,L]
</IfModule>
Note that this code has to be in the root directory of your subdomain. If, as you mentioned before, you've deleted this subdomain, you're going to need to re-establish it and put this .htaccess file in the root.
回答2:
This is the correct method if you wish to redirect all the pages and directories after that too:
RewriteCond %{HTTP_HOST} nz.business2sell.com$
RewriteRule ^(.*) http://www.business2sell.co.nz/$1 [R=301,L]
For example, nz.business2sell.com/businesses/
or nz.business2sell.com/broker_directory/
both redirects to www.business2sell.co.nz/businesses/
or /broker_directory/
This way no more pages are left behind
回答3:
Using Apache configuration directly is always preferred over .htaccess. Do you have access to that?
And do you want to redirect all users from the former subdomain to the index page of the main domain, or are you looking to map them page by page (for example my.domain.com/contact/ to www.domain.com/contact)?
回答4:
One thing you could do is go to the httpd.conf file and add this:
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain.domain.com
RewriteRule /(.*) http://www.domain.com [R=301,L]
That will point all pages from the subdomain at your main domain.
Good luck!
来源:https://stackoverflow.com/questions/12721334/how-to-set-all-pages-of-a-subdomain-to-redirect-to-a-single-page