【问题标题】:htaccess and multiple domainshtaccess 和多个域
【发布时间】:2011-12-14 22:29:32
【问题描述】:

场景:我在同一个 IP/机器上引导多个域,并交付依赖于请求/源域的内容。我没有使用基于名称的 v-hosts,因为我需要保持代码库的整合,以及其他杂项原因。我还需要为这些站点生成站点地图,并分别维护它们。我的困境是我正在尝试使用 htaccess 将所有 *sitemap.xml 和 *site_index.xml 请求重定向到其中包含请求域名的子文件夹,例如:

http://somedomain.com/sitemap.xml需要指向/var/www/html/sites/somedomain.com/sitemap.xml的本地目录

http://someotherdomain.com/sitemap.xml需要检索/var/www/html/sites/someotherdomain.com/sitemap.xml

这也必须在不更改地址栏中的地址或重定向搜索机器人等的情况下发生。

我已经研究过将所有 *sitemap.xml 请求定向到一个脚本以检索和提供所需的数据,但这似乎很麻烦,而且不像 htaccess 指令那样优雅。到目前为止,我已经制作了:

RewriteCond $1 !=网站 RewriteRule ^([^/]+)/sitemap.xml$ sites/%{HTTP_HOST}/sitemap.xml [L]

这不会导致错误,也不会产生预期的效果。有什么建议吗?

更新:我能够使用 Jacek 提供的指令,并对其进行一些修改以实现我所追求的。这就是我最终得到的结果:

RewriteRule ^sitemap.xml$ sites/%{HTTP_HOST}/sitemap.xml [L]

【问题讨论】:

标签: apache .htaccess mod-rewrite


【解决方案1】:

这是一次处理多个网站的更通用的答案:使用mapfile

首先,我建议将文件夹命名为所有“合作伙伴”或“网站”不带扩展名。

创建一个mapfile,在其中放置所有“合作伙伴”或“网站”,无需扩展名:

RewriteMap mappartners \
  dbm:/web/htdocs/one_for_all/rewriterules/partners.map

在地图文件中创建简单的条目,例如:

somedomain           one_for_all/somedomain
someotherdomain      one_for_all/someotherdomain
someotherotherdomain one_for_all/someotherotherdomain
...

那么困难的部分就在这里:

# Make a RewriteCond that fills "%1" with the name of the partner...
RewriteCond %{HTTP_HOST} ^(www\.)?([a-zA-Z0-9\-]+)\.+(fr|com|net|org|eu)$
# ...make a RewriteRule that does nothing but initialize "partner" variable:
RewriteRule (.*) - [QSA,E=PARTNER:${mappartners:%2|notfound}]
# If empty or "notfound"
RewriteCond %{ENV:PARTNER} ^$ [OR]
RewriteCond %{ENV:PARTNER} notfound
# "not found" 404 :
RewriteRule . - [R=404,L]

这样一切都是动态的,但有点复杂。

...并以完全重写目的地结束:

RewriteCond %{DOCUMENT_ROOT}/%{ENV:PARTNER}/%{REQUEST_FILENAME}  -f
# File exists => rewrite filename and end rewriterule:
RewriteRule  ^(.+) %{DOCUMENT_ROOT}/%{ENV:PARTNER}/%{REQUEST_FILENAME} [QSA,L]

-- 作为说明,我有一堆这样的 RewriteRules,我的第三个框架(几天后你会在我未来的网站上看到 (http://papdevis.fr))是最快的框架之一。

奥利维尔

【讨论】:

    【解决方案2】:

    应该大致可以工作:

    RewriteCond %{HTTP_HOST} !^(.*)$ [NC]
    RewriteRule ^sitemap.xml$ %1/sitemap.xml [L]
    

    主文件夹中的 .htaccess 和 someotherdomain.com 是子文件夹

    $1 等是来自 RewriteRule 的正则表达式捕获,而 %1-type 是来自 RewriteCond 的捕获

    【讨论】:

    • 我接受了您发布的内容,并且必须进行一些更改才能使其正常工作。我最终得到的是:RewriteRule ^sitemap.xml$ sites/%{HTTP_HOST}/sitemap.xml [L]我不确定为什么我不能让第二行像它一样工作,我遇到的大多数示例都包含非常相似的指令。无论如何,非常感谢!
    猜你喜欢
    • 2011-04-15
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多