Sandhurst Sandhurst - 2 months ago 20
Apache Configuration Question

.htaccess file mod_rewrite redirecting to parent directory instead of sub-directory when file names are same in both directories

I am accessing the root directory via

domain-A.com
and a sub-directory via
domain-B.com


All redirects are working properly for
domain-B.com
except those that are having
same names
for instance if root directory contains a file called
abc.html
and subdirectory also contains the file
abc.html
in that case accessing
domain-B.com/abc.html
shows the contents of
domain-A.com/abc.html
but the url remains the same i.e.
domain-B.com/abc.html


I would like to know how can this be resolved.
I am using Ubuntu and these are the settings that I have made for various file

I have already enabled mod_rewrite using
sudo a2enmod rewrite


.htaccess -
path /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 -
Path /etc/apache2/conf-available


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


000-default.conf -
Path /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 -
Path /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>

Answer

Issue is with these 2 conditions:

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

Which means if a file or directory exists in site root dir then don't rewrite to sub-directory.

Just change your rule to this:

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]