简体   繁体   English

使用htaccess将所有流量从子域重定向到域,而无需维护目录结构

[英]Use htaccess to redirect all traffic from subdomain to domain without maintaining directory structure

Most examples show how to redirect all subdomain traffic to a primary domain, maintaining the directory structure. 大多数示例说明如何将所有子域流量重定向到主域,并保持目录结构。 I actually don't want this. 我真的不想要这个。 I want to redirect all subdomain traffic (the site is going away) to the primary domain. 我想将所有子域流量(站点消失)重定向到主域。 This is not working: 这不起作用:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

What happens, is if you go to this: 发生的是,如果您转到此位置:

http://sub.newdomain.com/some/path/ http://sub.newdomain.com/some/path/

You get this: 你得到这个:

http://www.newdomain.com/some/path/ http://www.newdomain.com/some/path/

I want it all to go to the root. 我希望一切都扎根。

Just add a question mark to the end of the destination URL if you want to exclude all trailing info (eg. directory structure, parameters). 如果要排除所有尾随信息(例如,目录结构,参数),只需在目标URL的末尾添加一个问号即可。

In your instance, it would simply be updated to this: 在您的实例中,只需将其更新为:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/? [R=301,L]

How about a simple 301 redirect in the apache config file for the subdomain? 子域的apache配置文件中的简单301重定向怎么样?

To redirect ALL files on your domain use this in your .htaccess file if you are on a unix web server: 要重定向域中的所有文件,如果您使用的是UNIX Web服务器,请在.htaccess文件中使用以下命令:

redirectMatch 301 ^(.*)$ http://www.domain.com 
redirectMatch permanent ^(.*)$ http://www.domain.com

and another example 还有另一个例子

If you need to redirect http://mysite.com to http://www.mysite.com and you've got mod_rewrite enabled on your server you can put this in your .htaccess file: 如果您需要将http://mysite.com重定向到http://www.mysite.com,并且在服务器上启用了mod_rewrite,则可以将其放在.htaccess文件中:

EDIT: If you want to use this, just remove the $1 from the rules in the sample link provided if the first option above does not work. 编辑:如果要使用此功能,如果上面的第一个选项不起作用,只需从提供的示例链接中的规则中删除$ 1。

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/ [R=permanent,L]

or this: 或这个:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/ [R=301,L]

These excerpts from here 这些摘录来自这里

This handles wildcard subdomains, and the case where the client puts www before subdomain 这处理通配符子域,以及客户端将www放在子域之前的情况

#  wildcard.domain.com  ->  www.domain.com/wildcard
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %1 !^(www)$ [NC]
RewriteCond %1 !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/%1/ [R=301,L]

#   www.wildcard.domain.com   ->   www.domain.com/wildcard
RewriteCond %{HTTP_HOST} ^(www\.(.*))\.domain\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/%2 [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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