.htaccess文件mod_rewrite在两个目录

时间:2016-09-21 15:54:20

标签: apache .htaccess ubuntu mod-rewrite url-redirection

我通过domain-A.com访问根目录,通过domain-B.com访问子目录

如果根目录包含名为domain-B.com的文件且子目录中还包含文件same names,则abc.html除了那些abc.html以外的所有重定向都正常工作访问domain-B.com/abc.html的情况显示domain-A.com/abc.html的内容,但网址保持不变,即domain-B.com/abc.html

我想知道如何解决这个问题。 我正在使用Ubuntu,这些是我为各种文件设置的设置

我已经使用sudo a2enmod rewrite

启用了mod_rewrite

.htaccess - 路径/ var / www / html

# Do not change this line.
RewriteEngine on

# Change domain.com to be your primary main domain. http://domain-b.com/
RewriteCond %{HTTP_HOST} ^(www.)?domain-b.com$

# Change 'subfolder' to be the folder you want to redirect request to.
RewriteCond %{REQUEST_URI} !^/domain-b/

# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Change 'subfolder' to be the folder you want to use for your primary domain.
RewriteRule ^(.*)$ /domain-b/$1 [L]

# Change domain.com to be your primary domain again.
# Change 'subfolder' to be the folder you will use for your primary domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?domain-b.com$
RewriteRule ^(/)?$ domain-b/index.html [L]

httpd.conf - 路径/ etc / apache2 / conf-available

<Directory /var/www/html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

000-default.conf - 路径/ etc / apache2 / sites-available

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

     <Directory /var/www/html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
    </Directory>
</VirtualHost>

000-default.conf - 路径/ etc / apache2 / sites-enabled

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

     <Directory /var/www/html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
    </Directory>
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

问题在于以下两个条件:

# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

这意味着如果站点根目录中存在文件或目录,则不要重写到子目录。

只需将您的规则更改为:

RewriteEngine on

# Change domain.com to be your primary domain again.
# Change 'subfolder' to be the folder you will use for your primary domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www\.)?domain-b\.com$ [NC]
RewriteRule ^/?$ domain-b/index.html [L]

# Change domain.com to be your primary main domain. http://domain-b.com/
RewriteCond %{HTTP_HOST} ^(www\.)?domain-b\.com$ [NC]
# Change 'subfolder' to be the folder you want to redirect request to.
RewriteCond %{REQUEST_URI} !^/domain-b/ [NC]
# Change 'subfolder' to be the folder you want to use for your primary domain.
RewriteRule ^(.*)$ /domain-b/$1 [L]