使用mod_proxy重写显示给用户的URL

时间:2013-11-15 05:14:39

标签: .htaccess rewrite subdomain mod-proxy

我有一个WordPress网站,我需要重写某些页面的URL,以显示它们来自特定的子域以匹配现有的打印件。我试图通过htaccess和mod_proxy来做到这一点,我正在接近,但需要一些帮助。

以下是我现在的代码,它有些作用:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.edu$ [NC]
RewriteRule ^ http://othersubdomain.domain.edu/folder1/folder2/folder3%{REQUEST_URI} [L,P]

如果我输入subdomain.domain.edu,我会得到正确的页面(位于http://othersubdomain.domain.edu/folder1/folder2/folder3),浏览器URL仍然写为subdomain.domain.edu。这很棒!

问题在于内页。例如,如果我输入subdomain.domain.edu/contact,我会被重定向到正确的页面,但URL不会保持重写,它会显示其提供位置的完整URL。

有人可以帮我重写内页的网址吗?我觉得我太近了!

1 个答案:

答案 0 :(得分:2)

嗯,你这里主要没有使用mod_proxy。您正在使用mod_rewrite的代理标志。至少,您需要使用ProxyPassReverse来确保URL保持不变。

此外,在您的用例中不建议使用mod_rewrite。后端服务器返回的页面可能嵌入了绝对URL,直接引用其他的子域名.domain.edu。您应该使用mod_proxy的ProxyPass或ProxyPassMatch以及mod_proxy_html。如果您仍然无法弄明白,请在评论中告诉我。

<强>更新

首先,请花一些时间坐下来阅读the complete documentation about mod_proxy。它是最强大的apache模块之一,深入学习它将有很长的路要走。其次,不要使用.htaccess。

我假设您为特定的子域配置了虚拟主机。如果没有,您应该先配置它。现在,在您的虚拟主机配置中,添加以下行:

<VirtualHost *:80>
    ServerName subdomain.domain.edu

    ProxyPass         / http://othersubdomain.domain.edu/folder1/folder2/folder3/
    ProxyPassReverse  / http://othersubdomain.domain.edu/folder1/folder2/folder3/
    ProxyPassReverseCookieDomain  othersubdomain.domain.edu subdomain.domain.edu
    ProxyPassReverseCookiePath  /folder1/folder2/folder3/  /
</VirtualHost>

上述配置指令会将http://subdomain.domain.edu/foo/bar的请求代理为http://othersubdomain.domain.edu/folder1/folder2/folder3/foo/bar。当响应可用时,将更改其标头,以便用户看到它已从subdomain.domain.edu生成。如果您的后端服务器生成任何cookie(例如,如果用户必须登录),则需要Cookie指令。

这将带您走很远的路。之后,如果您想更改页面内容,嵌入式样式表或javascript中的链接,请参阅mod_proxy_html的文档。