Search code examples
phpapache.htaccessmod-rewritejoomla

Rewrite calls to index.php in redirected subdomain within .htaccess


I have two domains currently in my server, one for the main domain (www.domain.com) and the other one for the test domain (test.domain.com). Each domain points to a folder containing a Joomla installation and within each folder there is the stardard .htaccess file that comes with the installation.

The main domain works fine but the test domain does not quite work as expected. If I browse to test.domain.com I can see the Home page correctly but if I browse anywhere else (i.e. test.domain.com/products.html) I get a 500 error. However, if I append index.php after the subdomain it works as expected (i.e. test.domain.com/index.php/products.html).

This is the .htaccess file in /public_html folder:

Options +FollowSymLinks

RewriteEngine On
RewriteBase /

# pointing www.domain.com to live
RewriteCond %{HTTP_HOST} !test.domain.com
ReWriteCond %{HTTP_HOST} www.domain.com
ReWriteCond %{REQUEST_URI} !^/live/
ReWriteRule ^(.*)$ live/$1 [L]

# pointing test.domain.com to test
ReWriteCond %{HTTP_HOST} test.domain.com
ReWriteCond %{REQUEST_URI} !^/test/
ReWriteRule ^(.*)$ test/$1 [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]

# Rewrite any calls to /* to the index.php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php/$1 [L]

Here is the folder structure in the server:

/public_html
    |
    |--/live             <-- www.domain.com
    |     |
    |     |--.htaccess   <-- joomla standard .htaccess
    |
    |--/test             <-- test.domain.com
    |     |
    |     |--.htaccess   <-- joomla standard .htaccess
    |
    |--.htaccess

The .htaccess files in the live and test folders have RewriteBase /live and RewriteBase /test respectively.

I have found several questions regarding subdomain redirection here in stackoverflow and in many other sites but none of them solve this specific issue. Does anyone know what I am missing?

Thanks.


Solution

  • Have you tried to put subdomain rules before main domain? Here's my .htaccess that works fine for real project.

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} ^test\.domain.com [NC]
    RewriteRule ^.*$ index-test.php [NC,QSA] 
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^.*$ index.php