0

我很确定这部分与这个问题相反,但它是一个包含一些额外复杂性的两部分。

theorganization.com我最近用Joomla!为一个组织(我们称之为)继承了一个站点!安装后,有人刚刚anevent.com为它举办的活动购买了一个二级域名 ( ),期望我们可以直接将其“指向”我们现有网站的相关部分。是的,简单的“屏蔽”可以在一个方向上完成此操作,但我想规范化 URL 并使用等在两个方向上映射它。不是 无限循环:一个方向是 301 重定向;另一个是URL 掩码。)mod_rewrite

我想在我们现有的 Joomla 上托管有关活动的信息!安装在可访问的部分,theorganization.com/eventname/并与新域模棱两可。 注意: Joomla!使用重写本身,因此目录外壳中不会有物理目录。(不确定这是否/如何影响这里的任何事情。)eventnametheorganization.com

具体来说,我希望对或(带有或不带有斜杠或斜杠)提出的所有请求都为Joomla! 的内容提供服务。将URL 显示为. 当然,这些级别以下的所有子目录和文件也应该相应地映射。theorganization.com/eventnameanevent.comwwwtheorganization.com/eventname/www.anevent.com/


在浏览了Apache 的mod_rewrite文档之后,我想出了下面的尝试。

对于theorganization.com

RewriteEngine On

# STRIP "www" IF PRESENT (undesired, but necessary for now due to other issues)
RewriteCond %{HTTP_HOST} ^www.theorganization.com$ [NC]
RewriteRule ^(.*)$ http://theorganization.com/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/eventname/?(.*)$ [NC]
RewriteRule ^http://(www\.)theorganization.com/eventname/?(.*)$ "http://www.anevent.com/$2" [R=301,L] 

# ...
# ... default Joomla! .htaccess code here ...

经过多次尝试,我发现我忘记了 Joomla!默认情况下有自己的重写.htaccess,我无意中覆盖了它们。(谢天谢地,我找到了一个替代品。)但是第二个(有效的)重定向似乎没有像它应该的那样生效。我可能做错了什么?

对于anevent.com

RewriteEngine On

# ADD "www" IF MISSING
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# UNCONDITIONALLY DEFER ALL REQUESTS HERE TO theorganization.com/eventname/
RewriteRule ^(.*)$ http://theorganization.com/eventname/$1 [L]

这似乎工作得很好(基于我可以让第一个工作的假设)。但是,最后一行的无条件规则似乎充当 301/302 或类似的东西并加载http://theorganization.com/eventname/FOO。我该如何修改它以实现“屏蔽”,以便用户只能http://www.anevent.com/FOO在浏览器中看到但仍被定向到相同的内容?

4

1 回答 1

1

theorganization.com对于第一部分,将以下内容添加到站点根目录中的 .htaccess 文件中

RewriteEngine on
RewriteBase /

#if its on theorganization.com domain
RewriteCond %{HTTP_HOST} theorganization\.com$ [NC]
#if the original request has eventname in it
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /eventname/ [NC]
# redirect eventname to anevent.com
RewriteRule ^eventname/(.*)$ http://www.anevent.com/$1 [R=301,L]

对于 anevent.com:

您的规则有一个完全限定的 URL,因此它会自动重定向。我将其修改如下以进行服务器端重写,如下所示。

RewriteEngine on
RewriteBase /

#if not internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*)$ theorganization.com/eventname/$1 [L]
于 2012-01-19T23:13:28.293 回答