使用 mod_rewrite 进行双向 URL 映射和规范化
我很确定这在一定程度上与 这个相反问题,但这是一个由两部分组成的问题,有一些额外的复杂性。
我最近继承了一个组织的网站(我们将其称为 theorganization.com
),使用 Joomla ! 安装后,有人刚刚为它所主办的活动购买了辅助域名 (anevent.com
),期望我们可以将其“指向”我们的相关部分现有站点。是的,简单的“屏蔽”可以在一个方向上完成此操作,但我想规范化 URL 并使用 mod_rewrite
在两个方向上映射它>、等(这不是无限循环:一个方向是 301 重定向;另一个方向是 URL 掩码。)
我想将有关该事件的信息托管在我们现有的 Joomla!安装在 theorganization.com/eventname/
上可访问的部分,并使用新域来混淆它。 注意: Joomla!使用重写自身,因此 theorganization.com
目录中不会有物理目录 eventname
。 (不确定这是否/如何影响这里的任何内容。)
具体来说,我希望向 theorganization.com/eventname
或 anevent.com 发出所有请求>(带或不带
www
或尾部斜线)来提供 Joomla! 的内容通常会归因于 theorganization.com/eventname/
,同时将 URL 显示为 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
但仍被定向到相同的内容?
I'm pretty sure this is partly the opposite of this question, but it's a two-parter with a few extra intricacies.
I recently inherited a site for an organization (we'll call it theorganization.com
) with Joomla! installed and someone just went and bought a secondary domain (anevent.com
) for an event it's hosting with the expectation that we can just "point" it right at the relevant section of our existing site. Yes, simple "masking" could accomplish this in one direction, but I'd like to canonicalize the URL and map it in both directions using mod_rewrite
, etc. (This is not an infinite loop: one direction is a 301 redirect; the other is a URL mask.)
I want to host information on the event on our existing Joomla! installation at the section accessible at theorganization.com/eventname/
and equivocate this with the new domain. Note: Joomla! uses rewrites itself, so there will not be a physical directory eventname
within the directory housing theorganization.com
. (Not sure if/how that affects anything here.)
Specifically, I want all requests made to either theorganization.com/eventname
or anevent.com
(with or without www
or trailing slash) to serve the content which Joomla! would normally ascribe to theorganization.com/eventname/
while showing the URL as www.anevent.com/
. Of course, all subdirectories and files below these levels should map accordingly as well.
After looking through Apache's mod_rewrite
docs, I came up with the attempt below.
For 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 ...
After much trial, I found that I'd forgotten Joomla! has its own rewrites in .htaccess
by default and I had inadvertently overwritten them. (Thankfully, I found a replacement.) But the second (operative) redirect doesn't seem to be taking effect as it should. What might I be doing wrong?
For 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]
This seems to work pretty well (based on the assumption that I can get the first one to work). However, the unconditional rule on the last line seems to act as a 301/302 or something similar and loads up http://theorganization.com/eventname/FOO
. How can I modify this to achieve "masking" instead, so that users only see http://www.anevent.com/FOO
in their browser but are still directed to the same content?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一部分,将以下内容添加到
theorganization.com
网站根目录中的 .htaccess 文件对于 anevent.com:
您的规则具有完全限定的 URL,因此它将自动重定向。
我对其进行了如下修改以进行服务器端重写,如下所示。
For the first part, add the following to your .htaccess file in the root of
theorganization.com
siteFor anevent.com:
Your rule has a fully qualified URL so it will automatically redirect.
I modified it as below to do a server side rewrite as below.