apache - 当两个目录中的文件名相同时,.htaccess 文件 mod_rewrite 重定向到父目录而不是子目录

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

我通过domain-A.com访问根目录和一个子目录 domain-B.comdomain-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>

最佳答案

问题在于这两个条件:

# 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]

关于apache - 当两个目录中的文件名相同时,.htaccess 文件 mod_rewrite 重定向到父目录而不是子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39621287/

相关文章:

.htaccess - 删除子目录和文件扩展名

apache - 如何使用 .htaccess 检查缓存中是否存在文件,如果不存在则加载普通脚本

wordpress - HTTP 到 HTTPS - 重定向太多

ubuntu - 如何在 ubuntu 服务器上使用 Mozjpeg 批量优化图像?

git - 如何在 Bluehost 上安装 git 仓库

java - 如何从 pom.xml 中提取所有依赖项?

apache - 错误为属于同一域的文件夹设置 VirtualHost

android-studio - 使用 ubuntu snap 安装后 Flutter SDK 位置

node.js - 在ubuntu 14.04上执行dockerode npm的以下代码后出现错误 "connect EACCES/var/run/docker.sock"