简体   繁体   中英

Rewrite Rule to redirect everything in a directory to root of site

I just set up my first VPS. I have a primary domain - "mysite.com". I am hosting other websites within the directory of the primary domain. So that they are organized, I have placed them in a folder called "sites".

The problem is that i am able to reach the secondary sites by going to "mysite.com/sites/othersite/index.php as well as by the secondary domain's url.

I know I need to use .htaccess to fix this. so far I have tried the following after some research:

RewriteRule ^/sites/(.*)$ http://www.othersite.com/$1 [R=301,L]

This DOES rewrite mysite.com/sites/ back to the root url, but mysite.com/sites/othersite/index.php still produces a copy of othersite.com

I am looking for a rewrite rule or set of rules that will redirect absolutely any request for anything (files, subfolders, etc) in the /sites/ directory to the root directory (mysite.com)

That's not the way to do it. You should set up virtual hosts and set the webroot to separate folders.

Create a virtual host file for each site. And write an include statement for the entire directory in your httpd config.

Example of a virtual host file:

<VirtualHost *:80>
    ServerName domainname.com
    DocumentRoot /var/www/vhosts/domainname.com
    <Directory /var/www/vhosts/domainname.com>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
    </Directory>
    CustomLog /var/log/httpd/domainname.com-access.log combined
    ErrorLog /var/log/httpd/domainname.com-error.log
    LogLevel warn
</VirtualHost>

This will separate your error logs by domain

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