简体   繁体   中英

Apache Virtualhost Directory conditional redirect

I just created a website with two environments as virtualservers - testing and production. As production server is open to everyone but I allowed only my IP to access testing environment:

<VirtualHost *:80>
    DocumentRoot /home/xxx/www
    ServerName testing.xxx.com
    <Directory /home/xxx/www>
        Order deny, allow
        Deny from all
        Allow from xxx.xxx.xxx.xxx
    </Directory>
</VirtualHost>

The problem is that google has already indexed some of my testing environment pages and they are available in google results. I would like any IP but mine to be redirected to production server (xxx.com) while accessing testing.xxx.com. I would rather do it with apache.conf than .htaccess (because of git repositories conflicts). Is it possible to add a conditional redirect to apache config?

You can use mod_rewrite features in your httpd.conf Apache config file:

<IfModule mod_rewrite.c>

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REMOTE_HOST} !^123\.456\.788 [OR] # exclude your first IP
RewriteCond %{REMOTE_HOST} !^123\.456\.789 # exclude your second IP
RewriteRule ^(.*)$ http://production-env.com/$1 [R=301,L] # redirection to production site

</IfModule>

Or you can put these declarations into <Directory> section of your vhosts config file.

Generally you can take advantage of mod_rewrite module to manage URL routing policies for your web server. Before using it make sure that this module is installed and activated in your Apache.

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