using mod_rewrite to redirect from subdomain to maindomain

后端 未结 3 1662
挽巷
挽巷 2021-01-25 16:15

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

相关标签:
3条回答
  • 2021-01-25 16:28

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

    0 讨论(0)
  • 2021-01-25 16:37

    Why not try using a Redirect Directive from mod_alias?

    0 讨论(0)
  • 2021-01-25 16: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...

    0 讨论(0)
提交回复
热议问题