使用 mod_rewrite 从子域重定向到主域
我的问题是我有一个正常运行的子域(sub.mydomain.com)。 sub 已加载到 mydomain.com/sub 中。
我想做的是将所有对 sub.mydomain.com 的请求重定向到 mydomain.com 。
不知怎的,当我是子域时,我似乎无法访问根文件夹(主域)。我可以让它从 mydomain.com/sub 到 mydomain.com 工作。但不是来自子域。
目前我正在使用: <代码> RewriteEngine 上
RewriteRule ^(.*)/?$ /home/web/webuser/$1 [L] 访问
sub.mydomain.com 时,我收到 500 内部服务器错误。
从子系统访问主系统有限制吗? (权利方面) 或者也许是另一种进入主线的方式,也许类似于 (../$1)
谢谢
编辑: 我只能访问 .htaccess。因此,据我所知,DocumentRoot 不能在 .htaccess 文件中使用。 符号链接怎么样?我真的不知道它的作用,但我假设它链接两个位置?我找到的唯一启用符号链接的代码(Options +FollowSymlinks) - 但这行没有说明要链接的内容(也许我都错了)
顺便说一句。感谢迄今为止的投入!
My problem is that i have a functioning subdomain (sub.mydomain.com). sub is loaded in mydomain.com/sub.
What i would like to do is to redirect all requests to sub.mydomain.com to mydomain.com.
Somehow it seems that when im the subdomain i cannot access the rootfolder (main domain). I can get it working with from mydomain.com/sub to mydomain.com. But not from the subdomain.
Currently im using:
RewriteEngine on
RewriteRule ^(.*)/?$ /home/web/webuser/$1 [L]
When accessing sub.mydomain.com i get a 500 Internal Server Error.
Is there a restriction in accessing the main from a sub? (rights wise)
Or maybe another way of getting to main, perhaps something like (../$1)
Thanks
EDIT:
I only have access to .htaccess. So DocumentRoot cannot AFAIK be used in .htaccess file.
What about symlinks? I dont really know what it does, but i assume that it links two locations? The only code i found for that enables symlinks (Options +FollowSymlinks) - but this line doesnt say anything about what to link (perhaps im all wrong)
Btw. thanks for input so far !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我必须承认我没有完全理解你的问题。您想将所有内容从 sub.mydomain.com/whatever 重定向到 mydomain.com/whatever 吗?在这种情况下,以下内容(放入 sub.mydomain.com 的配置文件中)可能会起作用:
它在客户端上进行重定向,这意味着用户将在浏览器。
编辑:我想我现在明白你的问题了。是的,这是一个权限问题:如果您网站的 DocumentRoot 是 /whatever/sub,那么您不能通过在 URL 中添加“/..”来访问 /whatever。我希望你明白这是一件好事。 :-) mod_rewrite 只是更改 URL,因此它也不能这样做。
因此,要解决您的问题,您需要更改 sub.mydomain.com 的 DocumentRoot 或创建一个允许您访问所需目录的符号链接(例如 /whatever/sub/redir-target -> /whatever)。不过,要小心符号链接选项,因为它会在文件系统上创建无限长度的有效目录(/whatever/sub/redir-target/sub/redir-target/...),而某些脚本无法应对这一点。
编辑2:您可能想要创建一个别名,而不是符号链接,例如,如下所示:
不过,我认为最简单的解决方案是更改 DocumentRoot...
I must admit that I did not fully understand your question. Do you want to redirect everything from sub.mydomain.com/whatever to mydomain.com/whatever? In that case, the following (put in the config file of your sub.mydomain.com) might work:
It redirects on the client side, meaning that the user will see mydomain.com/sub in the browser.
EDIT: I think I understand your question now. Yes, it's a permissions issue: If the DocumentRoot of your web site is /whatever/sub, then you cannot just access /whatever by adding "/.." to the URL. I hope you understand that this is a good thing. :-) mod_rewrite just changes the URL, so it cannot do that either.
So, to solve your problem, you need to either change the DocumentRoot of sub.mydomain.com or create a symlink that allows you to access the required directory (e.g. /whatever/sub/redir-target -> /whatever). Be careful with the symlink option, though, since it will create valid directories of infinite length on your file system (/whatever/sub/redir-target/sub/redir-target/...) and some scripts cannot cope with that.
EDIT2: Instead of a symlink, you might want to create an alias, e.g., something like this:
Still, I think the easiest solution is to change the DocumentRoot...
为什么不尝试使用 mod_alias 的重定向指令?
Why not try using a Redirect Directive from mod_alias?
如果不了解有关服务器配置的更多信息,则很难提供明确的答案。
以下内容可能有效,并且至少是一个不错的起点:
理想情况下,它会放入您的 httpd.conf 中,但可能会在 .htaccess 文件中工作(同样,有关如何设置子域的更多信息会有所帮助)。
It's difficult to provide a definitive answer without knowing more about your server configuration.
The following might work and is at the very least a decent starting point:
Ideally that would go in your httpd.conf, but might work from a .htaccess file (again, more information about how your subdomains are setup would be helpful).