使用mod_rewrite从子域重定向到主域 [英] using mod_rewrite to redirect from subdomain to maindomain

查看:88
本文介绍了使用mod_rewrite从子域重定向到主域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我有一个正常运行的子域(sub.mydomain.com). sub已加载到mydomain.com/sub中.

My problem is that i have a functioning subdomain (sub.mydomain.com). sub is loaded in mydomain.com/sub.

我想做的是将所有对sub.mydomain.com的请求重定向到mydomain.com.

What i would like to do is to redirect all requests to sub.mydomain.com to mydomain.com.

以某种方式,当我在子域中时,我无法访问根文件夹(主域).我可以从mydomain.com/sub到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.

当前即时通讯正在使用: RewriteEngine on

Currently im using: RewriteEngine on

RewriteRule ^(.*)/?$ /home/web/webuser/$1 [L]

RewriteRule ^(.*)/?$ /home/web/webuser/$1 [L]

访问sub.mydomain.com时,我收到500内部服务器错误.

When accessing sub.mydomain.com i get a 500 Internal Server Error.

从子目录访问主目录是否受到限制? (明智的选择) 又或者是进入主站的另一种方式,例如(../$ 1)

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)

谢谢

我只能访问.htaccess.因此,无法在.htaccess文件中使用DocumentRoot. 符号链接呢?我真的不知道它的作用,但我认为它链接两个位置?我为此找到的唯一代码启用了符号链接(选项+ FollowSymlinks)-但此行未说明要链接的内容(也许全都错了)

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 !

推荐答案

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

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]

它在客户端上重定向,这意味着用户将在浏览器中看到mydomain.com/sub.

It redirects on the client side, meaning that the user will see mydomain.com/sub in the browser.

我想我现在已经理解您的问题了.是的,这是一个权限问题:如果您网站的DocumentRoot是/whatever/sub,那么您不能仅通过在URL上添加"/.."来访问/whatever.希望您了解这是一件好事. :-) mod_rewrite只是更改URL,因此也不能这样做.

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.

因此,要解决您的问题,您需要更改sub.mydomain.com的DocumentRoot或创建一个符号链接,以使您可以访问所需的目录(例如/whatever/sub/redir-target->/whatever) .但是,请小心使用symlink选项,因为它将在文件系统上创建无限长的有效目录(/whatever/sub/redir-target/sub/redir-target/...),并且某些脚本无法应对.

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.

您可能想创建一个别名,而不是符号链接,例如:

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

不过,我认为最简单的解决方案是更改DocumentRoot ...

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

这篇关于使用mod_rewrite从子域重定向到主域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆