using mod_rewrite to redirect from subdomain to maindomain

时间秒杀一切 提交于 2019-12-02 04:30:51

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

Why not try using a Redirect Directive from mod_alias?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!