using mod_rewrite to redirect from subdomain to ma

2019-03-03 05:08发布

问题:

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 !

回答1:

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



回答2:

Why not try using a Redirect Directive from mod_alias?



回答3:

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