gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:46:10 26 4
gpt4 key购买 nike

我通过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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com