使用 mod_rewrite 从子域重定向到主域

发布于 2024-08-09 23:02:36 字数 650 浏览 3 评论 0原文

我的问题是我有一个正常运行的子域(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2024-08-16 23:02:36

我必须承认我没有完全理解你的问题。您想将所有内容从 sub.mydomain.com/whatever 重定向到 mydomain.com/whatever 吗?在这种情况下,以下内容(放入 sub.mydomain.com 的配置文件中)可能会起作用:

    RewriteEngine On
    RewriteRule ^/(.*)$ http://mydomain.com/$1 [R,L]

它在客户端上进行重定向,这意味着用户将在浏览器。


编辑:我想我现在明白你的问题了。是的,这是一个权限问题:如果您网站的 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:您可能想要创建一个别名,而不是符号链接,例如,如下所示:

Alias /redir-target /home/web/webuser

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/redir-target/.*$
RewriteRule ^/(.*)$ /redir-target/$1

不过,我认为最简单的解决方案是更改 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:

    RewriteEngine On
    RewriteRule ^/(.*)$ http://mydomain.com/$1 [R,L]

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:

Alias /redir-target /home/web/webuser

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/redir-target/.*$
RewriteRule ^/(.*)$ /redir-target/$1

Still, I think the easiest solution is to change the DocumentRoot...

兮子 2024-08-16 23:02:36

为什么不尝试使用 mod_alias 的重定向指令?

Why not try using a Redirect Directive from mod_alias?

故事还在继续 2024-08-16 23:02:36

如果不了解有关服务器配置的更多信息,则很难提供明确的答案。

以下内容可能有效,并且至少是一个不错的起点:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.mydomain\.com 
RewriteRule (.*) /$1 [L]

理想情况下,它会放入您的 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:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.mydomain\.com 
RewriteRule (.*) /$1 [L]

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文