Home > Back-end >  htaccess RewriteRule gets error 500 with $1 for remapping subdomain
htaccess RewriteRule gets error 500 with $1 for remapping subdomain

Time:03-18

I have a shared hosting who points the main domain - mydomain.com and a subdomain - beta.mydomain.com - to the same folder public_html.

The structure of this hosting is:

/
a
b
public_html
 -a
 -b
 -.htaccess

The folder a and b inside public_html point to the respective folders in the root.

On the .htaccess file, there are these rules:

RewriteEngine on

#Subdomain rule#
RewriteCond %{HTTP_HOST} ^beta.mydomain.com$
RewriteRule ^(.*)$ /b/otherfolder/$1 [NC,L]

#Main domain rule#
RewriteCond %{REQUEST_URI} !^/a
RewriteRule ^(.*)$ /a/otherfolder/$1 [NC,L]

Actually, when I visit the main domain, the Main domain rule matches and it shows the right page.

When I visit the subdomain, the Subdomain rule matches and it returns error 500; however, removing the $1 on /b/otherfolder/$1, it shows the index file on that folder (of course, the page can't load the right js or css files).

Questions:

  1. How can I avoid the error 500 and let the Subdomain rule work as well as the other one?

  2. How can I enable the error log for the server/htaccess? I read about the ErrorLog directive, however my search only referred to the PHP.

CodePudding user response:

You are getting 500 because of infinite looping. Which is happening because subdomain rewrite is unconditional as it keeps rewriting to /b/otherfolder/.

You can have following rules:

RewriteEngine on

#Subdomain rule#
RewriteCond %{HTTP_HOST} ^beta\.mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/b/ [NC]
RewriteRule ^(.*)$ b/otherfolder/$1 [NC,L]

#Main domain rule#
RewriteCond %{REQUEST_URI} !^/(a|b)/ [NC]
RewriteRule ^(.*)$ a/otherfolder/$1 [NC,L]

Note addition of a condition RewriteCond %{REQUEST_URI} !^/b/ [NC] that stops this rewrite when URI starts with /b/.

  • Related