先进的.htaccess子域名重定向(foo.dom2.com到foo.dom1.com) [英] Advanced HTACCESS Subdomain Redirection (foo.dom2.com to foo.dom1.com)

查看:242
本文介绍了先进的.htaccess子域名重定向(foo.dom2.com到foo.dom1.com)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经domain1.tld和domain2.tld。 Domain1.tld是主域名,domain2.tld只是一个别名。我已经domain2.tld成功地重定向到domain1.tld,通过htaccess的,但我想在domain2.tld所有子域,以同样重定向到其对domain1.tld平行的子域,动态的。

例如:bla.domain2.tld应该重定向到bla.domain1.tld,并foo.domain2.tld应该重定向到foo.domain1.tld

这个解决方案需要是动态的,不是硬coded到特定的子域。

哈哈,我想我写是一个包罗万象的,单对单,子域包,别名域重定向脚本的.htaccess。

信息:

  • 我对BlueHost的,无法获得虚拟主机,httpd.conf中等等。

  • 我有一个字preSS子域网络(多点)。这意味着我的domain1.tld子域是虚拟的。字preSS处理这一点,所以只要我能得到foo.domain2.tld完全重定向到foo.domain1.tld,字preSS应该是没有最明智和正常工作。对吗?

  • 字preSS有专业域映射插件。这让我的网络上的网站有自己的域名。但手头的问题仅涉及我的网络的网站上的没有的拥有自己的域名,从而利用我的主域(domain1.tld)子域。

  • 因此​​,我有一个通配符子域设置为domain1.tld。

  • 不过,我也有domain2.tld通配符子域集,只是为了确保在domain2.tld所有子域都将在相同的IP地址domain1.tld:我的想法是说那么这些子域应该是在我的根目录下的.htaccess的可读性。

这是我的.htaccess,我很期待这样做的规则:

 的RewriteCond%{HTTP_HOST} ^(WWW \)?([A-Z0-9  - ] + \)DOMAIN2 \ .COM $ [NC]
重写规则^(WWW \。)([A-Z0-9  - ] + \。)HTTP \?(*):\ / \ /%2domain1 \ .COM \ / [L]
 

不过,这是行不通的。我在想什么?

我也尝试过使用一个环境变量,像这样的:

 的RewriteCond%{HTTP_HOST} ^(WWW \。)[E = subdm:([A-Z0-9  - ] + \)?DOMAIN2 \ .COM [NC ]
重写规则^ HTTP \(*):\ / \ /%{ENV:subdm} DOMAIN1 \ .COM \ / [L]
 

...但是这只是平原打破了这两个领域。 :D

我是比较精通正则EX pressions,所以我怎么舒服.htaccess的工作,但我其实挺新的世界的.htaccess。我需要另一双眼睛!

↓UPDATE(2011-11-08上午09点33分美国东部时间)↓

我尝试的第一个解决方案乔恩·林建议,并有轻微编辑。最初,它不会处理domain2.com没有一个子域,直到我把第二场比赛括号内对期,跟着对一个问号。

正如你可以看到下面,我已经提前,并提供我的.htaccess中的全部内容,如果你可以告诉大家,什么是相互矛盾的。

目前重定向正确使用建议的重写规则:www.domain2.com,domain2.com和domain2.com/foo 目前还没有与建议重写规则重定向:foo.domain2.com

 #使用PHP5单php.ini中的默认
AddHandler的应用程序/ x-的httpd-php5s的.php

选项​​+ SymLinksIfOwnerMatch
RewriteEngine叙述上

的RewriteCond%{HTTP_HOST} ^(WWW \)?([^ \。] + \。)?DOMAIN2 \ .COM $ [NC]
重写规则^ $ HTTP \(*):\ / \ /%2domain1 \ .COM \ / $ 1 [L,R]

#BEGIN字preSS
的RewriteBase /
重写规则^指数\ .PHP $  -  [L]

#上传的文件
重写规则^文件/(.+)WP-包括/ MS-files.php?文件= $ 1 [L]

的RewriteCond%{} REQUEST_FILENAME -f [OR]
的RewriteCond%{} REQUEST_FILENAME -d
重写规则^  -  [L]
重写规则。的index.php [L]
#结束字preSS
 

解决方案

你是在正确的轨道与%{HTTP_HOST},但你需要使用反向引用(%1%2,等等)来访问比赛反对。你想要做的事是这样的:

 的RewriteCond%{HTTP_HOST} ^(WWW \)?([^ \。] +)\。DOMAIN2 \ .COM $ [NC]
重写规则^(。*)$ HTTP://%2.domain1.com/$1 [L,R]
 

请注意,如果您尝试访问: http://www.sub.domain2.com/foo ,将您重定向到 http://sub.domain1.com/foo ,而WWW 。原始请求已经一去不复返了。如果你想在WWW,规则更改为:

 重写规则^(。*)$ HTTP://%1%2.domain1.com/$1 [L,R]
 

I have domain1.tld and domain2.tld. Domain1.tld is the primary domain name and domain2.tld is merely an alias. I have domain2.tld successfully redirected to domain1.tld, via HTACCESS, but I want all subdomains on domain2.tld to likewise redirect to their parallel subdomain on domain1.tld, dynamically.

For example: bla.domain2.tld should redirect to bla.domain1.tld, and foo.domain2.tld should redirect to foo.domain1.tld.

This solution needs to be dynamic, not hard-coded to specific subdomains.

Haha, I guess what I'm writing is a catch-all, one-to-one, subdomain-inclusive, alias domain redirection script in HTACCESS.

Information:

  • I am on Bluehost, without access to VHOSTS, HTTPD.CONF, etc.

  • I have a Wordpress subdomain network (multisite). This means my domain1.tld subdomains are virtual. Wordpress handles this, so as long I can get foo.domain2.tld fully redirected to foo.domain1.tld, Wordpress should be none-the-wiser and work properly. Right?

  • Wordpress has the "Professional Domain Mapping" plugin. This allows the sites on my network to have their own domain names. But the issue at hand only concerns sites on my network that don't have their own domain names, and thus utilize subdomains on my primary domain (domain1.tld).

  • Thus, I have a wildcard subdomain set up for domain1.tld.

  • But I also have a wildcard subdomain set for domain2.tld, just to make sure that all subdomains on domain2.tld are going to the same IP address as domain1.tld: my thinking being that then these subdomains should be readable by the HTACCESS in my root folder.

This is the rule in my HTACCESS that I'm expecting to do this:

RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+\.)?domain2\.com$ [NC]
RewriteRule ^(www\.)?([a-z0-9-]+\.)?(.*) http\:\/\/%2domain1\.com\/ [L]

But this isn't working. What am I missing?

I have also tried using an environmental variable, like this:

RewriteCond %{HTTP_HOST} ^(www\.)?[E=subdm:([a-z0-9-]+\.)]?domain2\.com [NC]
RewriteRule ^(.*) http\:\/\/%{ENV:subdm}domain1\.com\/ [L]

...but this just plains breaks both domains. :D

I'm relatively proficient with regular expressions, hence how comfortable I am working with HTACCESS, but I'm actually quite new to the world HTACCESS. I need another pair of eyes!

↓ UPDATE (2011-11-08 9:33am EST) ↓

I tried the first solution Jon Lin suggested, and had to edit it slightly. Originally, it wouldn't handle domain2.com without a subdomain until I put the period inside the second match parenthesis pair and followed the pair with a question mark.

As you can see below, I have gone ahead and provided the entire contents of my HTACCESS, in case you can tell that something is conflicting.

Currently redirecting properly with the suggested RewriteRule: www.domain2.com, domain2.com, and domain2.com/foo Currently NOT redirecting with the suggested RewriteRule: foo.domain2.com

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php

Options +SymLinksIfOwnerMatch
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+\.)?domain2\.com$ [NC]
RewriteRule ^(.*)$ http\:\/\/%2domain1\.com\/$1 [L,R]

# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
# END WordPress

解决方案

You're on the right track with %{HTTP_HOST} but you need to use backreferences (%1, %2, etc) to access the match against it. You want to do something like this:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain2\.com$ [NC]
RewriteRule ^(.*)$ http://%2.domain1.com/$1  [L,R]

Note that if you try to access: http://www.sub.domain2.com/foo it will redirect you to http://sub.domain1.com/foo, The "www." of the original request is gone. If you want the www, change the rule to:

RewriteRule ^(.*)$ http://%1%2.domain1.com/$1  [L,R]

这篇关于先进的.htaccess子域名重定向(foo.dom2.com到foo.dom1.com)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆