如何从子域上的文件夹重定向到主域
一些附加信息...
PHPBB 有一种方法可以在登录后将您重定向到您之前查看的页面,即使您在自己的页面上外部使用登录表单。然而,他们认为它应该是您自己域内的相对路径。如果您的论坛位于 forum.mydomain.com 等子域上,那么这并不是很好...
来自 他们的示例wiki:
为了解决子域的问题,我想我将添加一个指示frontpage 重定向到它,例如
因此,如果您在页面 http://mysite.com/frontpagenews.php
论坛重定向网址最终看起来像 forum.mysite.com/redirect/frontpagenews.php
(这有效,论坛创建此链接前往登录后返回)。它所需要的只是一个重写规则来检测这一点,这样它实际上会将您发送回 http://mysite.com/frontpagenews.php
问题...
我如何从 forum 访问.mysite.com/redirect/*anything*
到 mysite.com/*anything*
?
我已经这样做了一段时间了,我认为下面的代码应该可以工作,但事实并非如此。我已将其放在 forum.mysite.com 的 htaccess 文件中的其他论坛重写规则之上,因为如果它是检查的第一个规则,则可能应该没问题。
RewriteCond %{HTTP_HOST} ^forum\.mysite\.com/redirect/(.*)$ [NC]
RewriteRule (.*) http://mysite\.com/$1 [QSA,L,NC]
也尝试过将此作为条件,但没有喜悦:
RewriteCond %{REQUEST_URI} /redirect/(.*)$ [NC]
Bit of additional info...
PHPBB has a way to redirect you after login to the page you were looking at before, even if you're using the login-form externally on your own pages. However they decided it should be a relative path within your own domain. Which is not exactly great if you have the forum on a subdomain like forum.mydomain.com...
The example from their wiki: <input type="hidden" name="redirect" value="./somefile.html" />
To work around the problem of the subdomain, I figured I would add an indication of a frontpage redirect to it, like <input type="hidden" name="redirect" value="./redirect/frontpagenews.php" />
So if you use the login-form on the page http://mysite.com/frontpagenews.php
the forum redirect url ends up looking like forum.mysite.com/redirect/frontpagenews.php
(this works, the forum creates this link to go back to after login). All it needs is a rewrite rule to detect this so it would actually sent you back to http://mysite.com/frontpagenews.php
The question...
How do I go from forum.mysite.com/redirect/*anything*
to mysite.com/*anything*
?
I've been at this for a while now, and I think the code below should work, except it doesn't. I've put it above the other forum rewrite rules in the htaccess file in forum.mysite.com as it probably should be fine if it's the first rule that's checked.
RewriteCond %{HTTP_HOST} ^forum\.mysite\.com/redirect/(.*)$ [NC]
RewriteRule (.*) http://mysite\.com/$1 [QSA,L,NC]
Also tried this as condition, but no joy:
RewriteCond %{REQUEST_URI} /redirect/(.*)$ [NC]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTTP_HOST 仅匹配主机名,而不匹配路径。该路径需要是 RewriteRule 的一部分。假设您正在编辑的 .htaccess 文件位于 forum.mysite.com 根目录中(相当于:forum.mysite.com/.htaccess),它应该如下所示:
您不需要
QSA
在规则括号中,因为无论如何都会附加查询字符串(除非目标中有?
,例如http://mysite.com/$1?p=1
)。这会将浏览器从
forum.mysite.com/redirect/*anything*
重定向到mysite.com/*anything*
The HTTP_HOST only matches against a hostname, not the path. The path needs to be part of the RewriteRule. Assuming that the .htaccess file that you are editing resides in the forum.mysite.com root (as in the equivalent of: forum.mysite.com/.htaccess), it should look like this:
You won't need the
QSA
in the rule brackets because the query string will get appended anyways (unless you have a?
in the target, e.g.http://mysite.com/$1?p=1
).This will redirect the browser from
forum.mysite.com/redirect/*anything*
tomysite.com/*anything*