简体   繁体   中英

How to rewrite a domain to a subfolder

I have one local server called server1 with a subdirectory sub (server1/sub/). Now i have a dns entry which redirects server2 to server1.

I want to configure my apacher server in a way that when I open server2 in a browser I get the content from server1/sub/.

The url should not change to server1/sub/.

Is this possible with mod_rewrite?

EDIT: I added

127.0.0.1       localhost
127.0.0.1       wiki2
127.0.0.1       wiki3

to the hosts file and

VirtualHost 127.0.0.1>
  ServerName wiki2
  ServerAlias 127.0.0.1
  DocumentRoot c:/xampp/htdocs/wiki_angua

  <Directory c:/xampp/htdocs/wiki_angua >
     Allow From All
  </Directory>

</VirtualHost>

<VirtualHost 127.0.0.1>
  ServerName wiki3
  ServerAlias 127.0.0.1
  DocumentRoot c:/xampp/htdocs/weatherwax

  <Directory c:/xampp/htdocs/weatherwax >
     Allow From All
  </Directory>

</VirtualHost>

to httpd.conf and restarted apache. Whether I open wiki2 or wiki3 I land in ./wiki_angua. Is there anything I forgot?

If you have a DNS entry for server2 then the Host HTTP request header will be correctly set, and all you need then is a virtual host, without the need to use mod_rewrite.

For example:

<VirtualHost *:80>
  ServerName server2

  DocumentRoot /path/to/server1/sub

  <Directory /path/to/server1/sub>
     Allow From All
  </Directory>
  # ... etc
</VirtualHost>

edit :

In the case you still want to use mod_rewrite you can do something like:

RewriteCond   %{HTTP_HOST}                 ^server2$
RewriteRule   ^(.+)                        /path/to/server1/sub/$1

This must be located in the global server configuration, and not in an existing virtual host.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM