【问题标题】:.htaccess file mod_rewrite redirecting to parent directory instead of sub-directory when file names are same in both directories当两个目录中的文件名相同时,.htaccess 文件 mod_rewrite 重定向到父目录而不是子目录
【发布时间】:2017-01-29 23:36:12
【问题描述】:

我通过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 的内容,但 url 保持不变,即 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>

【问题讨论】:

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


    【解决方案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]
    

    【讨论】:

    • 它有效,但我不得不将 domain-b/index.html 文件更改为 domain-b/index.php 我不明白这种行为背后的原因,因为我们有这个 RewriteRule ^/?$ domain-b/index.html [L] 应该将用户带到 index.html 文件,但它一直在寻找 domain-b/index.php。
    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多